Created
August 11, 2021 20:35
-
-
Save sigmonky/c2e13c0a3e5780c96ed3cfb924aa5985 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import XCTest | |
struct LoggedInUser { | |
let name: String | |
} | |
class LoginViewController: UIViewController { | |
var login: (((LoggedInUser) -> Void) -> Void)? | |
var user: String? | |
func didTapLoginButton() { | |
guard let login = self.login else { | |
user = nil | |
return | |
} | |
login { [weak self] user in | |
self?.user = user.name | |
} | |
} | |
func showUser() { | |
print(user ?? "unknown") | |
} | |
} | |
let loginVC = LoginViewController() | |
let user = LoggedInUser(name: "randy") | |
loginVC.login = { completion in | |
completion(user) | |
} | |
loginVC.didTapLoginButton() | |
loginVC.showUser() | |
XCTAssertTrue(loginVC.user == "randy", "user value has wrong setting") | |
loginVC.login = { completion in | |
completion(LoggedInUser(name:"second user")) | |
} | |
loginVC.didTapLoginButton() | |
loginVC.showUser() | |
XCTAssertTrue(loginVC.user == "second user", "user value has wrong setting") | |
loginVC.login = nil | |
loginVC.didTapLoginButton() | |
loginVC.showUser() | |
XCTAssertTrue(loginVC.user == nil, "user value still set") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment