Andrew Lauer Barinov Andrew Lauer Barinov - 1 year ago 72
iOS Question

Using OCMockito to mock methods that are void

In using OCMockito, the below works great:

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

However when I try the same thing on a method with multiple arguments (see code below), I get an "Argument type 'void' is incomplete' compiler error.

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

Does anyone know the right way to go about this?


My original intent in asking this question was to resolve an issue of getting a compiler error when I try the following:

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

The method signature of the method I'm trying to mock is:

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

What I'm actually trying to do is to mock out the implementation of a
method. (Given a
method, mock out the implementation of the method with a block. For my purposes the method returns a completion block, that takes in two arguments. I would like to construct those two arguments and then run the completion block inside of the mocked out implementation block.)

Answer Source

OCMockito doesn't yet support stubbing of void methods. That's because until willThrow: and willDo: came along, there was no need. It will be added as a feature soon. You can track progress in

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download