Closed emadhegab closed 8 months ago
you should do call reset
first for myclass
, then call willReturn
, it should work
This is the expected behaviour, lets go through what happens in your example:
You store "1234" as the return value for customerNumber
given(myclass).customerNumber.willReturn("1234")
(Assuming you assert customerNumber
) You 'use' the return value by checking on it with an assertion, but since it is the only return value that is stored for that property, it is not removed from the return value queue so future assertions can use it if needed.
XCTAssert(Something)
You set an other return value (nil) for customerNumber
. After this, the return values are: ["1234", nil]
. This means that the first access will still get the value of "1234", which then will be removed from the return value queue as there is an other one (nil). The second access (and every subsequent) will get nil
.
given(myclass).customerNumber.willReturn(nil)
You can check this with the following:
given(myclass).customerNumber.willReturn("1234")
XCTAssertEqual(myclass.customerNumber, "1234")
given(myclass).customerNumber.willReturn(nil)
XCTAssertEqual(myclass.customerNumber, "1234")
XCTAssertEqual(myclass.customerNumber, nil)
As @vvisionnn said, you can use myclass.reset()
any time to empty all states from your mock class.
thanks
I have a scenario where i make given(myclass).customerNumber.willReturn("1234) XCTAssert(Something)
given(myclass).customerNumber.willReturn(nil)
i can see that the mock still see the first one only . it should replace or have a reset method.