2

Is it possible to have an actual object of a class and only mock a method in that class instead of mocking the whole object?

I want the object to behave 100% the same as the real object except 1 method.

Ex:

MyObject *object = [[MyObject alloc] init];
[[[object stub] andReturn:@"some_string"] getMyString];
aryaxt
  • 76,198
  • 92
  • 293
  • 442

2 Answers2

4

Yes, that's what partial mocks are for.

Partial mocks

id aMock = [OCMockObject partialMockForObject:anObject]

Creates a mock object that can be used in the same way as anObject. When a method that is not stubbed is invoked it will be forwarded to anObject. When a stubbed method is invoked using a reference to anObject, rather than the mock, it will still be handled by the mock.

Note that currently partial mocks cannot be created for instances of toll-free bridged classes, e.g. NSString.

See http://www.mulle-kybernetik.com/software/OCMock/

Community
  • 1
  • 1
Jonah
  • 17,918
  • 1
  • 43
  • 70
0

You can see on documentation: 10.3 Partial mocks cannot be created for certain special classes

Community
  • 1
  • 1
Cap
  • 2,004
  • 16
  • 17