1
運行代碼覆蓋率測試時,我注意到我沒有涵蓋用戶功能的錯誤處理。 功能測試:用戶錯誤的角度2因果單元測試
getVersion() {
return this.aboutService.getAPIVersion()
.subscribe(
info => {
console.log('info', info);
},
error => {
console.log('error', error);
}
);
}
這是功能是在組件和它調用的服務功能。我設法使用mockBackend abd MockError從服務中編寫函數的單元測試,但我不知道如何使用wrapper(調用者)函數來執行此操作。
到目前爲止,我嘲笑與一流的服務,但我只覆蓋響應,而不是錯誤:
class AboutServiceStub {
getAPIVersion = jasmine.createSpy('getAPIVersion').and.callFake(
this.fakedGetAPIVersion
);
fakedGetAPIVersion() {
console.log('fakedGetAPIVersion');
return Observable.of(new Object(version))
.map(version => JSON.parse(JSON.stringify(version)));
}
}
你是對的,只有在1)步驟你應該注入實際的服務,例如AboutService。當然,我應該導入'rxjs/add/observable/throw'。謝謝! – playerone
感謝評論我已編輯我的答案,以便它不會爲他人產生錯誤。 –