2016-12-28 80 views
0

我有方法,我想單元測試:IOS - 如何嘲弄與pmkpromise一個方法的返回類型

- (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]; 
}); 

} 
+0

你可以提供'getInfo'方法的定義嗎?我簡單地看了看PromiseKit和我發現的內容(https://github.com/mxcl/PromiseKit/search?utf8=%E2%9C%93&q=PMKPromise)我無法立即看到'then()'在哪裏定義。我會假設它是一個返回一個塊的屬性,但我不得不看看類型是什麼。 –

+0

感謝您的回覆Erik。我更新了我的問題,並添加了getInfo方法。 – user3158704

+0

你有什麼嘗試?你的測試案例是什麼樣的? –

回答

1

爲了測試這一點,你必須要麼打破getInfo和共享AMKServicesClient之間的依賴關係,或者建立某種制度,允許你當調用[AMKServicesClient sharedInstance]時加載模擬服務客戶端。