I'm having trouble using anyPointer to stub out methods that accept a pointer argument, such as the methods that take an NSError by reference. Here's an example:
- Code: Select all
id mockContext = [OCMockObject mockForClass:[NSManagedObjectContext class]];
[[mockContext stub] save:[OCMArg anyPointer]];
[[mockContext expect] save:[OCMArg anyPointer]];
NSError *error = nil;
[mockContext save:&error];
[mockContext verify];
And the result:
- Code: Select all
EXCEPTION AppDelegate saveChanges method should call save on managedObjectContext
OCMockObject[NSManagedObjectContext]: expected method was not invoked: save:0x1234567
If I specify a return value (as -save on NSManagedObjectContext does return a boolean success flag), as in the following test:
- Code: Select all
id mockContext = [OCMockObject mockForClass:[NSManagedObjectContext class]];
BOOL result = YES;
[[[mockContext stub] andReturn:OCMOCK_VALUE(result)] save:[OCMArg anyPointer]];
[[mockContext expect] save:[OCMArg anyPointer]];
NSError *error = nil;
[mockContext save:&error];
[mockContext verify];
I get an exception:
- Code: Select all
....PF.2010-10-18 10:58:51.158 UISpecs[85016:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Expected invocation with object return type.'
I feel like I may be writing this test incorrectly; am I? Any help would be appreciated!