Skip to content

Instantly share code, notes, and snippets.

@sigmonky
Created August 11, 2021 20:35
Show Gist options
  • Save sigmonky/c2e13c0a3e5780c96ed3cfb924aa5985 to your computer and use it in GitHub Desktop.
Save sigmonky/c2e13c0a3e5780c96ed3cfb924aa5985 to your computer and use it in GitHub Desktop.
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