Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c OCMock模拟协议';s的静态类方法。_Objective C_Unit Testing_Ocmock - Fatal编程技术网

Objective c OCMock模拟协议';s的静态类方法。

Objective c OCMock模拟协议';s的静态类方法。,objective-c,unit-testing,ocmock,Objective C,Unit Testing,Ocmock,OCMock 3中的新功能是模拟 可以模拟协议中定义的类方法吗?i、 e @protocol AViewControllerProtocol <NSObject> + (Type)typeForViewController; @end 异常抛出 NSInvalidArgumentException:无法存根/预期/验证方法“typeForViewController”,因为模拟类中不存在此类方法 这看起来像是OCMock 3.1中的一个疏忽,但是如果您愿意,您可以自己进行修复 //

OCMock 3中的新功能是模拟

可以模拟协议中定义的类方法吗?i、 e

@protocol AViewControllerProtocol <NSObject>
+ (Type)typeForViewController;
@end
异常抛出

NSInvalidArgumentException:无法存根/预期/验证方法“typeForViewController”,因为模拟类中不存在此类方法


这看起来像是OCMock 3.1中的一个疏忽,但是如果您愿意,您可以自己进行修复

// OCProtocolMockObject.m
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
{
    struct objc_method_description methodDescription = protocol_getMethodDescription(mockedProtocol, aSelector, YES, YES);
    if(methodDescription.name == NULL) 
    {
        methodDescription = protocol_getMethodDescription(mockedProtocol, aSelector, NO, YES);
    }
    // Add this case for required class methods
    if (methodDescription.name == NULL)
    {
        methodDescription = protocol_getMethodDescription(mockedProtocol, aSelector, YES, NO);
    }
    // Add this case for optional class methods
    if (methodDescription.name == NULL)
    {
        methodDescription = protocol_getMethodDescription(mockedProtocol, aSelector, NO, NO);
    }
    if(methodDescription.name == NULL)
    {
        return nil;
    }
    return [NSMethodSignature signatureWithObjCTypes:methodDescription.types];
}
我通过此测试验证了此修复:

- (void)testProtocolClassMethod {
    id mockedViewController = OCMProtocolMock(@protocol(AViewControllerProtocol));

    // FIXED: This line compiles fine, but throws an exception at run time.
    OCMStub([mockedViewController typeForViewController]).andReturn(SomeType);

    Type type = [mockedViewController typeForViewController];

    XCTAssertEqual(type, SomeType, @"Not equal!");

    OCMVerify([mockedViewController typeForViewController]);
}

我会在项目页面上对此提出请求。

我还没有时间回顾这个答案,但看起来很不错,谢谢。做得好。使用OCMock 2.2.4时也有同样的问题。我刚刚向master发布了一个修复程序。
- (void)testProtocolClassMethod {
    id mockedViewController = OCMProtocolMock(@protocol(AViewControllerProtocol));

    // FIXED: This line compiles fine, but throws an exception at run time.
    OCMStub([mockedViewController typeForViewController]).andReturn(SomeType);

    Type type = [mockedViewController typeForViewController];

    XCTAssertEqual(type, SomeType, @"Not equal!");

    OCMVerify([mockedViewController typeForViewController]);
}