import XCTest @testable import StudyCK class StudyCKTests: XCTestCase { var loginModel: LoginViewModel! = nil override func setUp() { super.setUp() loginModel = LoginViewModel() } override func tearDown() { // Put teardown code here. This method is called after the invocation of each test method in the class. loginModel = nil super.tearDown() } func testStartLogin() { let startExpectation = expectation(description: "login state changed to true") let endExpectation = expectation(description: "login state changed to false") loginModel.loginStateChanged = {(state: Bool) in if state { startExpectation.fulfill() } else { endExpectation.fulfill() } } loginModel.login() wait(for: [startExpectation, endExpectation], timeout: 10.0) } func testNoUsernameLogin() { loginModel.password = "aa" let expectation = XCTestExpectation(description: "no username") loginModel.showLoginFailedAlert = {(title: String, message: String) in if title == "錯誤" && message == "帳號不存在" { expectation.fulfill() } } loginModel.login() wait(for: [expectation], timeout: 10.0) } func testNoPasswordLogin() { loginModel.username = "ck1060535" let expectation = XCTestExpectation(description: "no password") loginModel.showLoginFailedAlert = {(title: String, message: String) in if title == "錯誤" && message.contains("密碼錯誤") { expectation.fulfill() } } loginModel.login() wait(for: [expectation], timeout: 10.0) } func testWrongPasswordLogin() { loginModel.username = "ck1060535" loginModel.password = "aa" let expectation = XCTestExpectation(description: "wrong password") loginModel.showLoginFailedAlert = {(title: String, message: String) in if title == "錯誤" && message.contains("密碼錯誤") { expectation.fulfill() } } loginModel.login() wait(for: [expectation], timeout: 10.0) } func testSuccessfulLogin() { loginModel.username = "tUsername" loginModel.password = "tPassword" let expectation = XCTestExpectation(description: "login successful") loginModel.loginSuccessfully = { expectation.fulfill() } loginModel.login() wait(for: [expectation], timeout: 10.0) } }