我正试图弄清楚如何在下面的代码中测试403响应。代码按预期工作。我意识到使用403并不是最好的选择,但这已经超出了我的控制范围,我无法改变它。我是JS测试的新手,感觉自己错过了什么,已经到了知识的尽头。如有任何帮助,我们将不胜感激。
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;
};
这是我到目前为止的测试。我不知道如何实现这个承诺。我可能在这次测试中有更多的废话,但我现在太失落了,我不知道。
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);
});
});
我已经为您的问题设置了一个jsfiddle。在测试中,我简化了您的POST url。注意事项:
-
当
expectPOST
从$httpBackend
返回时,您应该返回数据响应(因为您在实际服务中这样做)。例如:$httpBackend.whenPOST('/path/to/your/url') .respond(403, yourErrorResponseData);
-
因为
$http
已经返回了一个承诺,所以您不需要返回defer.promise
-
在测试中,您可以使用promise测试在$httpBackend中返回的数据。并且不要忘记调用
$httpBackend.flush
来刷新请求。myService.joinItem(item, joinOption).then(function (data) { expect(data).toEqual(joinedItemResults); }); $httpBackend.flush();