我正在尝试测试此控制器功能:
$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 } ].
尝试$scope.$digest()
之后打电话deleteAccount
。这是必需的,因为测试没有摘要周期与在浏览器中正常运行一样的摘要周期。
$scope.deleteAccount($scope.accounts[0]);
$scope.$digest();
expect($scope.accounts).toEqual([{_id: 2}, {_id: 3}]);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句