Cuckoo icon indicating copy to clipboard operation
Cuckoo copied to clipboard

when().thenReturn() always return nil

Open alopezh opened this issue 5 years ago • 3 comments

I'm trying to make getExpositionInfo() to return a value like this

        stub(expositionInfoRepository!) { stub in
            
            let expositionInfo = ExpositionInfo(level: .Healthy)
            when(stub.getExpositionInfo()).thenReturn(expositionInfo)
           
         }

When the call to the stubbed method is done inside the sut it returns nil

I've been debugging and when setting up the return value:

public extension StubFunctionThenReturnTrait {
    @discardableResult
    func thenReturn(_ output: OutputType, _ outputs: OutputType...) -> Self {
        ([output] + outputs).forEach { output in
            stub.appendAction(.returnValue(output))
        }
        return self
    }
}

output parameter is always nil, it look like it's overwrited somewhere.

Is there anything wrong with my code?

Regards

alopezh avatar Aug 21 '20 12:08 alopezh

Hey @alopezh, could you please include the mocked type? I'll try to reproduce your issue, but if I fail, I will need you to either fork Cuckoo and add the test case that covers this issue or create a minimal project where this issue occurs.

MatyasKriz avatar Aug 22 '20 16:08 MatyasKriz

Hi,

Te mocked type is a protocol:

protocol ExpositionInfoRepository {
    func getExpositionInfo() -> ExpositionInfo?
    func save(expositionInfo: ExpositionInfo)
    func clearData()
}

ExpositionInfo is the value that is always nil:

struct ExpositionInfo: Codable, Equatable {

    var level: Level
    var lastCheck: Date?
    var since: Date?
    var error: DomainError?

    public init(level: Level) {
        self.level = level
    }

    enum Level: String, Codable {
         case healthy
         case exposed
         case infected
    }

}

alopezh avatar Aug 26 '20 09:08 alopezh

@MatyasKriz any hepl on this?

Thank you

alopezh avatar Sep 11 '20 18:09 alopezh