Brightify / Cuckoo

Boilerplate-free mocking framework for Swift!
MIT License
1.67k stars 174 forks source link

No stub for method error #383

Closed iadcialim closed 3 years ago

iadcialim commented 3 years ago

This test is working

func testExample1() throws {

    let mock = MockSampleUseCase()
    let viewModelSpy = MockSampleViewModel(useCase: mock).withEnabledSuperclassSpy()

    let response = User()
    let login = Observable.just(response)
    let request = LoginRequest()
    request.username = "iad"

    stub(mock) { stub in
        when (stub.login(request: any())).thenReturn(login)
    }

    viewModelSpy.login(name: "iad")

    verify(viewModelSpy).loginSuccess()
}

This one is throwing "No stub for method login(request: LoginRequest) -> Observable<User> using parameters sampletest.LoginRequest. (NSInternalInconsistencyException)"

func testExample2() throws {

    let mock = MockSampleUseCase()
    let viewModelSpy = MockSampleViewModel(useCase: mock).withEnabledSuperclassSpy()

    let response = User()
    let login = Observable.just(response)
    let request = LoginRequest()
    request.username = "iad"

    stub(mock) { stub in
        when (stub.login(request: request)).thenReturn(login)
    }

    viewModelSpy.login(name: "iad")

    verify(viewModelSpy).loginSuccess()
}

The difference is on the first one, I am passing any() while in the 2nd its a concrete class. I'm suspecting its because of my extension of LoginRequest to conform to Matchable

extension LoginRequest: Matchable {
    public var matcher: ParameterMatcher<LoginRequest> {
        return equal(to: self)
    }
    public typealias MatchedType = LoginRequest
}

Source code: sampletest.zip

iadcialim commented 3 years ago

@TadeasKriz can u guide me? thanks a lot

iadcialim commented 3 years ago

Never mind I have solved this one. My matcher is checking if LoginRequest variables are of the same instance. In my case, it does not have to be. As long as they have the same username value, then I consider as the same.

extension LoginRequest: Matchable {
    public var matcher: ParameterMatcher<LoginRequest> {
        return ParameterMatcher<LoginRequest>() { loginRequest in
            return loginRequest.userName == self.userName
        }
    }
    public typealias MatchedType = LoginRequest
}