首页 > 解决方案 > 没有在 OCMStub/OCMExpect/OCMReject 中记录调用

问题描述

我试图在我的一个单元测试中模拟一个 Singleton 类。我创建模拟的方式是这样的:

MySingletonController *mockController = OCMClassMock([MySingletonController class]);
OCMStub([MySingletonController sharedController]).andReturn(mockController);

我得到的完整错误是:

Did not record an invocation in OCMStub/OCMExpect/OCMReject.
Possible causes are:
- The receiver is not a mock object.
- The selector conflicts with a selector implemented by OCMStubRecorder/OCMExpectationRecorder. (NSInternalInconsistencyException).

值得一提的是,共享实例不是实例化类的东西,它只是返回一个变量。该变量是在 nib 加载期间的 init(发生)期间分配的。但我真的怀疑这与问题有关。

我不完全知道什么可能导致OCM我看到的错误。

标签: objective-cxctestocmock

解决方案


问题是我如何声明OCMStub. 我的sharedController方法是一种class方法。然后它需要以不同的方式声明OCMStub

MySingletonController *mockController = OCMClassMock([MySingletonController class]);
OCMStub(ClassMethod([(id)mockController sharedController])).andReturn(mockController);

如上所述更改我的代码后,测试开始正常工作。


推荐阅读