OCMockito进行存根方法

时间:2018-12-28 10:16:56

标签: objective-c stubbing ocmockito

嗨,我在测试运行时发送对象消息的方法时遇到问题,测试失败并显示此错误:

failed: caught "NSInvalidArgumentException", "*** -[NSProxy doesNotRecognizeSelector:sendMessage:] called!"

这是我的测试方法:

- (void)testSendingRequests
 {
        MNRequest *request = [MNRequest requestWithType:@"AUselessType"
                                                    content:@"AUselessContent"
                                                   delegate:nil
                                                    timeOut:10
                                                   userInfo:nil];

            MNMessage * message = mockClass([MNMessage class]);
            [given([mockMsgCenter sendMessage:message]) willDo:^id(NSInvocation *invocation) {
                // Mock implementation goes here
        }
        NSNumber *identificationNumber = [center sendRequest:request];
        verify(mockMsgCenter);
        // some other code
  }

这是错误的屏幕: enter image description here

1 个答案:

答案 0 :(得分:0)

这是问题的解决方法

MNMessage * message = mockClass([MNMessage class]);
    [given([mockMsgCenter sendMessage:message]) willDo:^id(NSInvocation *invocation) {
        // Mock implementation goes here
        NSArray *args = [invocation mkt_arguments];
        MNMessage * msg = args[0];
        MNPackagedRequest *pkgRequest = (MNPackagedRequest *)[args[0] content];
        BOOL c1 = [msg.type isEqualToString:MNRequestMessageType];
        BOOL c2 = [[pkgRequest type] isEqualToString:@"AUselessType"];
        BOOL c3 = [(NSString *)[pkgRequest content] isEqualToString:@"AUselessContent"];
        BOOL c4 = [[msg content] isEqual:pkgRequest];
        BOOL c5 = [msg guaranteedDelivery];
        BOOL c6 = ([msg ttl] == request.timeOut);
        return @(c1 && c2 && c3 && c4 && c5 && c6);
    }];
     NSNumber *identificationNumber = [center sendRequest:request];
     verify(mockMsgCenter);