I have been trying to use the 'andReturnValue' method to fake results, but I am experiencing odd behaviour. When I pass the results directly to another function or method, everything is fine, but when I assign the result to a variable, the result is 0. Here's my code (I'm mocking (or more precisely, faking) the complaintCount method of an IComplaintCollection protocol, which should return an NSInteger.)
- Code: Select all
- (void) testAddAnotherComplaintButtonIsHiddenIfCompliantCollectionHasTwoComplaints{
//Initialise mock object
OCMockObject<IComplaintCollection>* complaintCollection = [OCMockObject niceMockForProtocol:@protocol(IComplaintCollection)];
// Set up the complaintCount method to return an NSInteger of 2
NSInteger numberOfComplaints = 2;
[[[complaintCollection expect] andReturnValue:[NSValue value:&numberOfComplaints withObjCType:@encode(NSInteger)]] complaintCount];
// Test passing the result to a function - works as expected
STAssertEquals([complaintCollection complaintCount], 2, @"Expect should make method return 2");
// Test putting the result in a variable - fails as variable becomes 0
NSInteger numberOfComplaintsFromComplaintCount = [complaintCollection complaintCount];
STAssertEquals(numberOfComplaintsFromComplaintCount, 2, @"Expect when result of method is put in variable it should equal 2");
}
I'm using the source code which I downloaded last Wednesday. OCMock is a target referenced by my test project. Am I doing anything wrong? I experienced the same problem when I tried using 'andDo' and returning an object via the NSInvocation.
Thanks,
Polly
