How do I stub a class method with OCMockito?

半腔热情 提交于 2019-12-06 08:57:30

Your assertion statement is using NSString directly. Use the mock instead:

assertThat([stringClassMock string], equalTo(@"Purple"));

In other words, the mock isn't swizzling the actual NSString. Rather, it's a stand-in for it.

EDIT:

In your edited example, your "methodThatCallsAClassMethod" has a dependency on NSString. If you wanted to replace that, you either need to inject the class, or Subclass and Override Method.

Injection:

return [self.stringClass string];

Then you can set the stringClass property to a mock.

Subclass and Override Method:

@interface TestingFoo : Foo
@end

@implementation @TestingFoo

- (NSString *)methodThatCallsAClassMethod
{
    // return whatever you want
}

@end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!