[英]Test progress with a promise notify for $httpBackend
我正在使用https://github.com/danialfarid/angular-file-upload進行文件上傳。 當xhr請求接收到progress
事件時,它提供了一個調用的progress
方法。 這是來自angular-file-upload的源代碼:
xhr.upload.addEventListener('progress', function(e) {
deferred.notify(e);
}, false);
我的問題是,現在應該如何使用$httpBackend
測試它? 我可以用以下方法測試成功和錯誤的方法
$httpBackend.expectPOST("http://localhost:9001/").respond('ok');
$httpBackend.expectPOST("http://localhost:9001/").respond(500, 'some error');
但我無法得到開除諾言的notify
。 有沒有辦法做到這一點?
編輯
我要測試的部分位於progress
方法中:
$upload.http({url: url, method: 'POST', data: file})
.progress(function(evt) {
// here is more code, that needs to be tested
self.$deferreds.upload.notify((100.0 * evt.loaded / evt.total).toFixed(2));
}).success(function(data, status, headers, config) {
self.$deferreds.upload.resolve(data);
}).error(function(response) {
self.$deferreds.upload.reject(response);
});
您想監視$upload.http
方法並返回一個模擬對象,該對象可讓您注冊進度,成功和錯誤回調。
spyOn($upload, 'http').andReturn({
progress: function (progressCallback) {
this.progressCallback = progressCallback;
},
success: function (errorCallback) {
this.errorCallback = errorCallback;
},
error: function (errorCallback) {
this.errorCallback = errorCallback;
}
});
然后,您可以在測試中同步調用這些回調:
it('should do something', function () {
$upload.progressCallback();
// or $upload.successCallback();
// or $upload.errorCallback();
expect('this').toBe('that');
});
我們說的是單元測試?
為什么根本需要測試第三方?
嘲笑這一切 。
可能我沒有想到這個問題,因此需要更多應由測試覆蓋的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.