Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. // You implememt a Mock which conforms to the same protocol the ViewController is connected to
  2. class UserServiceMock: UserServiceInterface {
  3.  
  4. var resultToReturn: Bool = false
  5. var errorToReturn: Error?
  6.  
  7. func login(email: String, password: String, completion: (Bool, Error?)) {
  8. completion(resultToReturn, errorToReturn)
  9. }
  10. }
  11.  
  12. class LoginTests: XCTestCase {
  13.  
  14. let viewControllerToTest = ViewController()
  15.  
  16. func testLoginFailed() {
  17.  
  18. let mockService = UserServiceMock()
  19. mockService.resultToReturn = false
  20. mockService.errorToReturn = Error("no internet")
  21.  
  22. viewControllerToTest.userService = mockService
  23.  
  24. // 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
  25. let loginExpectation = expectation(description: "should call login")
  26. viewController.login("testEmail", "testPassword", completion: { success in
  27. loginExpectation.fulfill()
  28. XCTAssertFalse(success, "the login should fail")
  29. })
  30.  
  31. wait(for: loginExpectation, timeout: 0.1)
  32. }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement