1

在使用 OCMockito 时,以下效果很好:

DSAPIManager *mockAPIManager = mock([DSAPIManager class]);
[given([mockAPIManager initWithBaseURL:[mockAPIManager baseURL]]) willReturn:[DSAPIManager sharedAPIManager]];

但是,当我在具有多个参数的方法上尝试相同的操作时(请参见下面的代码),我收到“参数类型 'void' 不完整”编译器错误。

DSAPIManager *mockAPIManager = mock([DSAPIManager class]);
[given([mockAPIManager setLoginCredentialsWithEmail:@""
                                           password:@""]) willReturn:@""];

有谁知道这样做的正确方法?

编辑

我提出这个问题的初衷是解决在尝试以下操作时出现编译器错误的问题:

[given([mockAPIManager setLoginCredentialsWithEmail:@"" password:@""]) willDo:^id(NSInvocation *invocation) {
        // Mock implementation goes here
}];

我试图模拟的方法的方法签名是:

- (void)setLoginCredentialsWithEmail:(NSString *)email password:(NSString *)password;

我实际上想要做的是模拟一个void方法的实现。(给定一个void方法,用一个块模拟该方法的实现。出于我的目的,该方法返回一个完成块,它接受两个参数。我想构造这两个参数,然后在模拟的内部运行完成块出实施块。)

4

2 回答 2

3

现在你可以像这样使用 givenVoid

[givenVoid([mockAPIManager setLoginCredentialsWithEmail:@"" password:@""]) willDo:^id(NSInvocation *invocation) {
        // Mock implementation goes here
}];
于 2016-11-11T15:28:03.530 回答
2

OCMockito 还不支持 void 方法的存根。那是因为直到willThrow:willDo:出现,没有必要。它将很快作为一项功能添加。您可以在https://github.com/jonreid/OCMockito/pull/93中跟踪进度

于 2015-03-16T04:59:06.700 回答