objective-c - 没有在 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
我看到的错误。
解决方案
问题是我如何声明OCMStub
. 我的sharedController
方法是一种class
方法。然后它需要以不同的方式声明OCMStub
。
MySingletonController *mockController = OCMClassMock([MySingletonController class]);
OCMStub(ClassMethod([(id)mockController sharedController])).andReturn(mockController);
如上所述更改我的代码后,测试开始正常工作。
推荐阅读
- java - 如何在 java 中使用 utf-8 字符串而不分配新的 String 对象,而是作为字节数组的一部分?
- cmake - CMake 无法编译“针对符号重定位 R_X86_64_PC32”
- security - 如何管理智能家居的服务帐户密钥 json 文件
- java - 在 Java 中创建哈希
- python - 从烧瓶中动态创建的文件夹中渲染模板
- c++ - 如何在主线程和库回调之间共享互斥锁?
- asp.net - 如何从母版页上的用户控件访问母版页中定义的数据集?
- react-native - 使用嵌套导航器时,initialRouteName 无法正常工作
- node.js - 如何将节点加密导入 Angular 11 组件?
- amazon-web-services - java.lang.NoClassDefFoundError: clojure/lang/IFn in aws lambda