Search code examples
iosocmock

OCMock andReturn for different value based on the same mock instance, unexpected return value.


See #if 0...#endif, if enable then the mock works as expected, return 1 when invoking method preferencesGeneralUnits, if disable then the mock still returns the previous value 0 despite we ask him to return 1.

My question, is it OCMock bug? Or is it expected usage for OCMock? Thank you.

Bad for both 2.0.1 and build from latest source code.

Any idea or discussion will be appreciated, thanks in advance.

- (void) testFormattedDistanceValueWithMeters{
    id mockSettings = [OCMockObject mockForClass:[TnSettings class]];
    id mockClientModel = [TnClientModel createMockClientModel];
    [[[mockClientModel stub] andReturn:mockSettings] settings];

    [[[mockSettings stub] andReturn:[NSNumber numberWithInt:0]] preferencesGeneralUnits];

    NSNumber *meters = [NSNumber numberWithDouble:0.9];
    distance = [NSString formattedDistanceValueWithMeters:meters];
    STAssertEqualObjects(distance, @"0.9", @"testformattedEndTimeForTimeInSeconds failed");

    //--------------See here---------------------
    #if 0
    mockSettings = [OCMockObject mockForClass:[TnSettings class]];
    mockClientModel = [TnClientModel createMockClientModel];
    [[[mockClientModel stub] andReturn:mockSettings] settings];
    #endif
    [[[mockSettings stub] andReturn:[NSNumber numberWithInt:1]] preferencesGeneralUnits];

    meters = [NSNumber numberWithDouble:1];
    distance = [NSString formattedDistanceValueWithMeters:meters];
    STAssertEqualObjects(distance, @"3.3", @"testformattedEndTimeForTimeInSeconds failed");

    [TnClientModel releaseInstance];
    }

Solution

  • Erik Doernenburg's explain is reasonable.