这是我正在测试的控制器:
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.');
});
};
}
我的测试:
describe('AdminController', function() {
var createController, scope, $httpBackend;
beforeEach(module('mean-starter'));
beforeEach(module('templates'));
beforeEach(inject(function($controller, $rootScope, _$httpBackend_) {
$httpBackend = _$httpBackend_;
scope = $rootScope.$new();
createController = function() {
return $controller('AdminController');
}
}));
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
it('gets users', function() {
var users = [ 'user1', 'user2', 'user3' ];
$httpBackend
.expectGET('/users')
.respond(users);
var adminController = createController();
$httpBackend.flush();
expect(adminController.users).toEqual(users);
});
it('has a method that deletes users', function() {
$httpBackend.expectDELETE('/users/1').respond();
$httpBackend.expectGET('/current-user').respond();
$httpBackend.expectGET('/logout').respond();
var adminController = createController();
adminController.delete(1);
$httpBackend.flush();
});
});
它目前失败:
它希望我$httpBackend.expectGET('/users')
在我的第二个it
街区有一个。我不想那样做。it
对我来说,如果每个块测试一个单独的东西,它似乎更加模块化和有组织。
1)我该怎么办?
2)为什么我会收到$digest already in progress
错误消息?