Created
September 25, 2019 19:20
-
-
Save catalinaturlea/1d388859cf5d49cb47b8030ac9cb6073 to your computer and use it in GitHub Desktop.
Tests-version2
This file contains hidden or 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
// You implememt a Mock which conforms to the same protocol the ViewController is connected to | |
class UserServiceMock: UserServiceInterface { | |
var resultToReturn: Bool = false | |
var errorToReturn: Error? | |
func login(email: String, password: String, completion: (Bool, Error?)) { | |
completion(resultToReturn, errorToReturn) | |
} | |
} | |
class LoginTests: XCTestCase { | |
let viewControllerToTest = ViewController() | |
func testLoginFailed() { | |
let mockService = UserServiceMock() | |
mockService.resultToReturn = false | |
mockService.errorToReturn = Error("no internet") | |
viewControllerToTest.userService = mockService | |
// Since this is an async call we need to wait for the callback in the test - otherwise we will not run long enought to get the response | |
let loginExpectation = expectation(description: "should call login") | |
viewController.login("testEmail", "testPassword", completion: { success in | |
loginExpectation.fulfill() | |
XCTAssertFalse(success, "the login should fail") | |
}) | |
wait(for: loginExpectation, timeout: 0.1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment