开发者

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜