OCMock 版本 3 将无法识别 class 方法

OCMock version 3 will not recognized class methods

我正在尝试在 class 方法上使用 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);

当我 运行 测试时出现错误

failed: caught "NSInvalidArgumentException", "-[OCMockObject(PGWS3ObjectMask) maskWithObjectType:]: unrecognized selector sent to instance 0x7fe420e0ca60"

你通过 OCMClassMock(cls) 得到的是一个实例对象,而不是 class,即

id mockMask = OCMClassMock([ObjectMask class]);

只是 returns 个实例。


如果您想调用 ObjectMask 的 class 方法 +maskWithObjectType:,请尝试

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