Search code examples
objective-cunit-testingocmock

OCMock and overriding stub value


mockModule = OCMPartialMock(module);
OCMStub([mockModule send:@"FOO"]).andReturn(YES);
OCMStub([mockModule send:@"FOO"]).andReturn(NO);

In this example I have a simple mock module, and I set some stubs to return YES/NO when sent a String, the problem that occurs is that if I set the same string twice it only returns the first value, and not the new value.

In this example about the problem is demonstrated like so I would expect a call such as:

BOOL answer = [module send:@"FOO"]
//answer should be NO, but is YES

How can I make it respond with the most recently set value?


Solution

  • You could use the expect methods, e.g.

    mockModule = OCMPartialMock(module); OCMExpect([mockModule send:@"FOO"]).andReturn(YES); OCMStub([mockModule send:@"FOO"]).andReturn(NO);

    That's not exactly what they are meant for, but it does make some sense. You're basically saying, I expect that send: will be called, and when that has actually happened, then I want the method to be stubbed.

    Also, if it were possible to "pile up" the stubs, figuring out what went wrong would be quite difficult, e.g. if the first invocation of the stub doesn't happen, then the second invocation will get the value meant for the first.