iosobjective-ciphoneocmock

How to partial mock objects of the same class twice in OCMock 3.4.2?


EKSource *source1 = [[EKSource alloc] init];
EKSource *source2 = [[EKSource alloc] init];

id source1Mock = OCMPartialMock(source1);
[[[source1Mock stub] andReturnValue:@(EKSourceTypeBirthdays)] sourceType];

id source2Mock = OCMPartialMock(source2);
[[[source2Mock stub] andReturnValue:@(EKSourceTypeCalDAV)] sourceType];

NSLog([source1 sourceType]); # Getting EKSourceTypeLocal instead of EKSourceTypeBirthdays
NSLog([source2 sourceType]); # Getting EKSourceTypeCalDAV. Expected behaviour.

I'm trying to partial mock two objects of EKSource class as shown above. After mocking source2 object, source1Mock stops mocking. How to solve this problem ? I did not notice this issue in OCMock 3.4.1.


Solution

  • Your implementation is correct. There's a bug in version 3.4.1 of OCMock. The bug was fixed in version 3.4.3 - see release notes.

    I suggest using macros to stub methods because they are much more intuitive to write and easier to read.

    e.g. OCMock([source1 sourceType]).andReturn(@(EKSourceTypeBirthdays))