stumped
stumped

Reputation: 3293

How to mock a class method of NSObject using OCMock

Is there a way to create an instance of an NSObject? I'm trying to mock a class method from NSObject and am getting an error that objc[86140]: no class for metaclass 0x1065c0e38.

- (void)testChainMethodCalled
{
  id controller = [OCMockObject partialMockForObject:[MyController class]];
  MyController *controller = controllerId;

  id object = [[OCMockObject partialMockForObject:[NSObject alloc] init] ];
  [[[object expect] classMethod] cancelPreviousPerformRequestsWithTarget:self selector:@selector(chainMethod) object:nil];

  [controller callTestMethod];
  OCMVerifyAll(object);
}

Upvotes: 2

Views: 1486

Answers (1)

bplattenburg
bplattenburg

Reputation: 696

When mocking class methods, you should be using OCMClassMock. You're using partialMockForObject which mocks the object instance and not the class itself.

For your example:

id objectMock = OCMClassMock([NSObject class]);

Then you can mock any method on the class like such:

OCMStub(ClassMethod([objectMock classMethod])).andReturn(mockValue)

Where classMethod is the class method you're mocking and mockValue is the fake value you're mocking out for that class method

See http://ocmock.org/reference/#mocking-class-methods for more details on this

Upvotes: 1

Related Questions