测试 Angular 错误回调中的服务解析承诺
Testing Angular Service resolve promise in error callback
我正在尝试找出如何在以下代码中测试 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
时,您应该 return 数据响应(因为您在实际服务中这样做)。例如:
$httpBackend.whenPOST('/path/to/your/url')
.respond(403, yourErrorResponseData);
因为$http
已经return一个承诺,所以你不需要returndefer.promise
在您的测试中,您可以使用promise 测试您在$httpBackend 中return 的数据。并且不要忘记调用 $httpBackend.flush
来刷新请求。
myService.joinItem(item, joinOption).then(function (data) {
expect(data).toEqual(joinedItemResults);
});
$httpBackend.flush();
我正在尝试找出如何在以下代码中测试 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
时,您应该 return 数据响应(因为您在实际服务中这样做)。例如:$httpBackend.whenPOST('/path/to/your/url') .respond(403, yourErrorResponseData);
因为
$http
已经return一个承诺,所以你不需要returndefer.promise
在您的测试中,您可以使用promise 测试您在$httpBackend 中return 的数据。并且不要忘记调用
$httpBackend.flush
来刷新请求。myService.joinItem(item, joinOption).then(function (data) { expect(data).toEqual(joinedItemResults); }); $httpBackend.flush();