0

I am trying to write test cases for the test target. The app is running perfectly, but the test case failing with the mocking. Here is my code, please let me know if there any other solutions. How do I mock services?

Is there any other way to write a test case for the ViewCoontroller initializer?

Error

    
class NavigationCodeTests: XCTestCase {
    var subject: ViewController?

    override func setUp() {
        self.subject = ViewController(service: MockUserService())
        _ = self.subject?.view
    }

    func test_user_service_not_nil() {
        XCTAssertNotNil(self.subject?.service, "User service can't be nil after initialization of ViewController")
    }

    func test_user_service_should_have_user() {
        self.subject?.userViewModel?.user.observe(on: self, observerBlock: { (user) in
            XCTAssertNotNil(user?.name, "User service can't be nil after initialization of ViewController")
        })
    }
}

class MockUserService: UserService  {
    func fetch(_ completion: @escaping(_ user: User) -> Void) {
        completion(User(name: "abc", contact: "124"))
    }
}

class UserService: UserServiceDelegate  {
    func fetch(_ completion: @escaping(_ user: User) -> Void) {
        completion(User(name: "Damu", contact: "12"))
    }
}

protocol UserServiceDelegate {
    func fetch(_ completion: @escaping(_ user: User) -> Void)
}

DRN
  • 153
  • 2
  • 12
  • The code you've shown and the one you paste isn't the same. `class MockUserService` doesn't seem to inherit from `UserService` in your screenshot, but does in your code, which seem to be why the compiler error is thrown. – Larme Feb 09 '21 at 10:20
  • @Larme yes, I did that mistake. sry for that. but in both cases same error I'm getting. – DRN Feb 09 '21 at 10:24
  • Isn't `ViewController` the same as what we get from UIKit? I can only assume you meant a specific subclass of your own that takes a `service:` in the initializer. Can you fill out the example a bit more? – Jon Reid Feb 13 '21 at 19:38

0 Answers0