Use of [OCMArg anyPointer]

Discussion of the OCMock framework. If you have patches we would prefer you to send them to the mailing list, but attaching them to a topic is possible, too.

Use of [OCMArg anyPointer]

Postby harryb » 18 Oct 2010, 17:03

Hello,

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!
harryb
 
Posts: 2
Joined: 18 Oct 2010, 16:54

Re: Use of [OCMArg anyPointer]

Postby erik » 21 Oct 2010, 09:22

The issue with the first snippet is that you're using stub and expect. The call that's made is handled by the stub and never makes it to the expect. If you want to verify a method is called only use expect, there's no need to also stub the method.

I have to look into the second issue later.
erik
 
Posts: 90
Joined: 10 Oct 2009, 15:22
Location: Hamburg, Germany

Re: Use of [OCMArg anyPointer]

Postby dave » 15 Aug 2011, 22:42

Anything new on this topic? I'm too trying to mock the save: method of NSManagedObjectContext, but

Code: Select all
id mockContext = [OCMockObject mockForClass:[NSManagedObjectContext class]];
[[mockContext stub] save:[OCMArg anyPointer]];


doesn't even compile using Xcode 4.2 with ARC enabled...

Avoiding the existence of a full-fledged CoreData stack in my unit tests is one of the main reasons for me to use a mocking framework after all. I know that many people create an in-memory store for their tests, but I think there's no point of taking that configuration overhead if you are just testing the code above the data layer. So a mock for NSManagedObjectContext seems like a perfect fit for that.
dave
 
Posts: 1
Joined: 15 Aug 2011, 21:59


Return to OCMock



cron