[英]Testing Angular Service resolve promise in error callback
I am trying to figure out how to test the 403 response in the following code. 我试图找出如何在以下代码中测试403响应。 The code works as expected.
该代码按预期工作。 I realized using 403 is not the best option here but that is out of my control right now and I cant change it.
我意识到在这里使用403并不是最好的选择,但是现在这已经超出了我的控制范围,我无法更改。 I am new to JS testing an feel like I am missing something and have reached the end of my knowledge.
我是JS测试的新手,感觉好像我缺少了一些东西,已经达到了我的知识水平。 Any help would be appreciated.
任何帮助,将不胜感激。
this.joinItem = function(item, joinOption) {
var deferred = $q.defer(),
joinUrl = ('http://testurl.com/company/items/abc456/join', joinData),
joinData = {
match: {
minid: joinOption['id']
}
};
$http.post(joinUrl, joinData, { headers: headers })
.success(handleJoinSuccess)
.error(handleJoinError);
function handleJoinSuccess() {
deferred.resolve({status: 204});
};
function handleJoinError(joinedResponse, status) {
var joinedItems = joinedResponse['joined_items'];
if (status === 403) {
deferred.resolve({
joinedItems: joinedItems,
status: status
});
} else {
deferred.reject({
status: status
});
}
}
return deferred.promise;
};
Here is the test I have so far. 这是我到目前为止的测试。 I have no idea how to get this promise to resolve.
我不知道如何实现这一诺言。 I may have more crap in this test than needed but I am so lost right now I have no idea.
我在这项测试中可能会比需要的更多,但是我现在很茫然,我不知道。
describe('itemService', function () {
'use strict';
var subject;
beforeEach(inject(function (_itemService_) {
subject = _itemService_;
}));
describe('#joinItem 403', function () {
var $httpBackend,
$rootScope,
deferred,
item,
joinOption,
joinData,
joinedResponse,
joinRequest,
joinedItemsResults,
joinedItems;
beforeEach(inject(function(_$httpBackend_, $q, _$rootScope_) {
$httpBackend = _$httpBackend_;
$rootScope = _$rootScope_;
deferred = $q.defer();
item = { id: 'abc456' };
joinOption = { id: 'optID' };
joinData = { match: { minid: 'optID' } };
joinedResponse = { 'joined_products': []};
joinRequest = $httpBackend.whenPOST('http://testurl.com/company/items/abc456/join', joinData).respond(403);
joinedItemsResults = { joined_products: [], status: 403 };
}));
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
describe('when request completes with 403 status', function () {
it('resolves the promise', function () {
var handler = jasmine.createSpy('error');
var promise = subject
.joinItem(item, joinOption);
promise.then(handler);
deferred.resolve(joinedResponse);
$rootScope.$digest();
$httpBackend.flush();
expect(promise).toBeResolvedWith(joinedItemsResults);
});
});
I have setup a jsfiddle for your problem. 我为您的问题设置了一个jsfiddle 。 In the test I have simplified your POST url.
在测试中,我简化了您的POST网址。 Something to notice :
注意事项:
You should return data response(Because you do that in your real service) when expectPOST
from $httpBackend
. 当
expectPOST
从$httpBackend
时,您应该返回数据响应(因为您在真实的服务中这样做)。 Ex : 例如:
$httpBackend.whenPOST('/path/to/your/url') .respond(403, yourErrorResponseData);
Because $http
already return a promise, so you dont need to return defer.promise
因为
$http
已经返回了一个promise,所以您不需要返回defer.promise
In your test, you can test the data that you return in $httpBackend by using promise. 在测试中,您可以使用promise测试在$ httpBackend中返回的数据。 And dont forget to call
$httpBackend.flush
to flush request. 并且不要忘记调用
$httpBackend.flush
刷新请求。
myService.joinItem(item, joinOption).then(function (data) { expect(data).toEqual(joinedItemResults); }); $httpBackend.flush();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.