我试图测试这个控制器功能:
$scope.deleteAccount = function(account) {
Account.deleteAccount(account._id)
.then(function() {
angular.forEach($scope.accounts, function(a, i) {
if (a === account) {
$scope.accounts.splice(i, 1);
}
});
})
.catch(function(err) {
$scope.errors.other = err.message;
});
};
这是一个管理页面上。该函数调用工厂(承诺),工厂删除服务器上的帐户。然后该函数删除范围内的元素,以便删除的元素不再显示。
我的测试看起来像这样:
beforeEach(inject(function ($controller, $rootScope, _$location_, _$httpBackend_) {
$scope = $rootScope.$new();
$location = _$location_;
$httpBackend = _$httpBackend_;
fakeResponse = '';
AdminAccountCtrl = $controller('AdminAccountCtrl', {
$scope: $scope
});
$location.path('/admin/account');
}));
it('test delete account', function() {
expect($location.path()).toBe('/admin/account');
$httpBackend.expectGET('/api/accounts').respond([{_id: 1}, {_id: 2}, {_id: 3}]);
$httpBackend.when('GET', 'app/admin/admin.account.html').respond(fakeResponse);
$httpBackend.when('DELETE', '/api/accounts/1').respond(fakeResponse);
$httpBackend.flush();
$scope.deleteAccount($scope.accounts[0]);
expect($scope.accounts).toEqual([{_id: 2}, {_id: 3}]);
});
可悲的结果是:
Expected [ { _id : 1 }, { _id : 2 }, { _id : 3 } ] to equal [ { _id : 2 }, { _id : 3 } ].
而......你的问题是什么? – Aracthor
第一个元素未被删除 - 测试失败。 我想知道为什么数组没有改变。 –