Kolos65 / Mockable

A Swift macro driven auto-mocking library.
MIT License
233 stars 18 forks source link

When make more than 1 given to same property it persists the first one #12

Closed emadhegab closed 8 months ago

emadhegab commented 8 months ago

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.

vvisionnn commented 8 months ago

you should do call reset first for myclass, then call willReturn, it should work

Kolos65 commented 8 months ago

This is the expected behaviour, lets go through what happens in your example:

  1. You store "1234" as the return value for customerNumber given(myclass).customerNumber.willReturn("1234")

  2. (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)

  3. 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.

emadhegab commented 8 months ago

thanks