Mocking NSApp, other class methods

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.

Mocking NSApp, other class methods

Postby carpeaqua » 15 Feb 2010, 23:18

Hi

I am trying to using OCMock to test a window controller that drops down a sheet, but am running into an issue with class methods such as +[NSApplication sharedApplication] and how to use them with OCMock.

I want to do something like this, but it doesn't seem possible since endSheet:returnCode is called from [NSApplication sharedApplication]:

- (IBAction)doSomething:(id)sender
{
// Do our stuff here
[NSApp endSheet:[self window] returnCode:NSOKButton];
}

- (void)testTheStuffWeDid
{
id mockApp = [OCMockObject mockForClass:[NSApplication class]];
[[mockApp expect] endSheet:[OCMArg any] returnCode:NSOKButton];
[mockApp verify];
}

Any suggestions on a best practice for a situation like this? Thanks!
carpeaqua
 

Re: Mocking NSApp, other class methods

Postby chrispix » 16 Jun 2011, 18:29

Justin, I wrote up a method for doing this at http://twobitlabs.com/2011/02/mocking-singletons-with-ocmock/. I'm also looking at how OCMock might be extended to mock class methods using the runtime.
chrispix
 
Posts: 7
Joined: 07 May 2010, 19:32


Return to OCMock



cron