Objective C - OCMock and stubbing?
Is it possible to have an actual obje开发者_运维百科ct 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];
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/
You can see on documentation: 10.3 Partial mocks cannot be created for certain special classes
精彩评论