I am using a third party framework to send data to a server. I am mocking that third party framework to isolate and test how my code interacts with it. This is to avoid to avoid waiting to get back data from the network while unit testing, and because I want to test the error handling code that I've written.
I am not using OCMock or anything like it, I am writing my own mock object by subclassing the 3rd party framework.
My method looks like this-
- (void)loginWithCredentials:(NSDictionary *)credentials
{
NSDictionary *credentials = [self credentials];
NSString *username = [credentials objectForKey:kUserCredintialUsername];
NSString *password = [credentials objectForKey:kUserCredintialPassword];
[ThirdPartyClass loginWithUsername:username
andPassword:password
block:^(ThirdPartyClass *user, NSError *error){
if (user) {
NSLog(@"We logged in");
}
else if (error) {
NSLog(@"%@", [error errorString]);
}
}];
}
What I'd like to do is to is to call loginWithUsername:andPassword:block:
on my mock in my unit tests. The current method is, obviously, untestable because it doesn't follow "tell, don't ask" (I'm not telling loginWithCredentials:
which class to call loginWithUser...block:
on). The only way that I can think to solve this is to add a class variable to my instance or class or add an argument to loginWithCredentials:
so I can substitute my mock in. The production code doesn't gain any clarity by doing so - it is already being written as an adapter to the 3rd party library. Instead, I would prefer to try to refactor the loginWithCredentials:
method.