OCMock版本3无法识别类方法

时间:2015-12-08 02:45:27

标签: ios testing ocmock

我正在尝试在类方法上使用OCMock版本3。这些方法无法识别。有谁知道为什么以及如何解决这个问题。当我键入存根的代码时,不会出现maskWithObjectType方法。无论如何我输入它并尝试测试。一些代码是

#import <Foundation/Foundation.h>
@interface ObjectMask : NSObject
@property (strong, nonatomic, readonly) Class objectType;
@property (strong, nonatomic, readonly) NSMutableArray* requestedFields;
@property (strong, nonatomic, readonly) NSDictionary* children;
@property (weak, nonatomic, readonly) ObjectMask* parent;
#pragma mark - Factory
+(instancetype)maskWithObjectType:(Class)objectType;

模仿是

id mockMask = OCMClassMock([ObjectMask class]);
OCMStub([mockMask maskWithObjectType:[someotherclass class]]);
[mockMask maskWithObjectType:[someotherclass class]];
OCMVerify(mockMask);

当我运行测试时,我收到错误

  

失败:捕获&#34; NSInvalidArgumentException&#34;,&#34; - [OCMockObject(PGWS3ObjectMask)maskWithObjectType:]:无法识别的选择器发送到实例0x7fe420e0ca60&#34;

1 个答案:

答案 0 :(得分:3)

OCMClassMock(cls)得到的是一个实例对象,而不是一个类,即

id mockMask = OCMClassMock([ObjectMask class]);

只返回一个实例。

如果要调用ObjectMask的类方法+maskWithObjectType:,请尝试

id mockMaskForSomeOtherClass = [ObjectMask maskWithObjectType:[someotherclass class]];