$httpBackend.expect()
所有的请求都是由您的代码完成的。如果他们中的任何一个没有,我会抛出异常。"
1)这似乎是多余的。是不是相应的$httpBackend.expect()
会失败,从而提醒你呢?
2)当我使用No response defined !
时,我收到$httpBackend.verifyNoOutstandingExpectation()
错误。当我删除$httpBackend.verifyNoOutstandingExpectation()
时,错误消失了,我的测试通过了。为什么会这样?
admin.controller.spec.js:14
是指$httpBackend.verifyNoOutstandingExpectation()
来电。
admin.controller.spec.js
describe('AdminController', function() {
var AdminController, scope, $httpBackend;
beforeEach(module('mean-starter'));
beforeEach(inject(function($controller, $rootScope, _$httpBackend_) {
$httpBackend = _$httpBackend_;
scope = $rootScope.$new();
AdminController = $controller('AdminController', {
$scope: scope
});
}));
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
// $httpBackend.verifyNoOutstandingRequest();
});
it('gets users', function() {
$httpBackend.expectGET('/users');
});
});
admin.controller.js
angular
.module('mean-starter')
.controller('AdminController', AdminController);
function AdminController(User, Auth, $state) {
var vm = this;
User
.list()
.success(function(data) {
vm.users = data;
})
.error(function() {
console.log('Problem getting users.');
});
vm.delete = function(id) {
User
.delete(id)
.success(function(data) {
if (Auth.getCurrentUser()._id === id) Auth.logout(); // deleting yourself
else $state.reload();
})
.error(function() {
console.log('Problem deleting user.');
});
};
}
答案 0 :(得分:2)
错误消息清楚地说明了问题 - 您需要先定义预期的响应,然后才能验证期望是否已满足。
更改
$httpBackend.expectGET('/users');
到
$httpBackend.expectGET('/users').respond([{
//Response object or array
}]);