ios - How to mock a method with pmkpromise as the return type

169 views Asked by At

I have method which I want to unit test:

- (void)fetchInfo {
[AMKAccountService getInfo]
.then(^(AMKInfoResponse *response) {

    if (response.accounts.count > 0) {
        _viewModel = [[AMKInfoViewModel alloc] initWithInfoResponse:response];
        [self.view setAsOfDate:_viewModel.asOfDate];
    } else {
        [self.view showError:[AMKStrings feedbackForCode:@"testError"]];
    }
}).catch(^(NSError *error) {
    DLog(@"Error getting info: %@", error);
    [self.view showError:[AMKStrings feedbackForCode:@"testError"]];

});

}

In this method, 'getInfo' method makes a service call and returns response of type PMKPromise object.

My question is how to mock the getInfo method and make the 'then' block called for one unit test and 'catch' block called for the other unit test.

[Update] Here's getInfo method:

+ (PMKPromise *)getInfo {
AMKServicesClient *client = [AMKServicesClient sharedInstance];

return [client GET:@"/amk-web-services/rest/info" parameters:nil].thenInBackground(^(NSDictionary *responseDictionary) {

    return [[AMKInfoResponse alloc] initWithResponse:responseDictionary];
});

}
1

There are 1 answers

0
Daniel T. On

In order to test this, you will have to either break the dependency between getInfo and the shared AMKServicesClient, or set up some sort of system that allows you to load a mock services client when [AMKServicesClient sharedInstance] is called.