0

我有我想单元测试的方法:

- (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"]];

});

}

在此方法中,“getInfo”方法进行服务调用并返回 PMKPromise 对象类型的响应。

我的问题是如何模拟 getInfo 方法并为一个单元测试调用“then”块,为另一个单元测试调用“catch”块。

[更新] 这是 getInfo 方法:

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

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

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

}
4

1 回答 1

1

为了测试这一点,您将不得不打破getInfo和 shared之间的依赖关系AMKServicesClient,或者设置某种系统,允许您在[AMKServicesClient sharedInstance]调用时加载模拟服务客户端。

于 2017-05-08T13:14:10.193 回答