Question

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];
Was it helpful?

Solution

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/

OTHER TIPS

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top