使用 OCMockito 模拟无效的方法

Using OCMockito to mock methods that are void

在使用 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 方法,用一个块模拟该方法的实现。为了我的目的,方法 returns 一个完成块,它接受两个参数。我想构造这两个参数和然后 运行 模拟实现块内的完成块。)

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

中跟踪进度

现在你可以像这样使用 givenVoid

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