我正在使用AngularJS 1.2.10。 我的应用程序工作,问题只出在单元测试中。 使用$ httpBackend执行更新(HTTP PUT)时,不会调用回调,因此我的测试失败
我想测试的控制器方法如下:
$scope.updateName = function(name, release) {
$scope.isLoading = true;
release.name = name;
release.$update({ projectId: $scope.projectId }, function() {
$scope.isLoading = false;
}, function(err) {
console.log(err);
});
};
它使用的服务是:
angular.module('app.release').factory('ReleaseService', ['$resource', function($resource) {
return $resource('projects/:projectId/releases/:releaseId', {
releaseId: '@_id'
}, {
update: {
method: 'PUT'
}
});
}]);
单元测试:
it('$scope.updateName() should update release name', inject(function(ReleaseService) {
var data = function(){
return {
_id: '456',
name: 'v1.0',
description: 'A Release'
};
};
// mock release object
var release = new ReleaseService(data());
scope.release = release;
scope.projectId = '123';
$httpBackend.expectPUT('projects/123/releases/456').respond({
_id: '456',
name: 'a new name',
description: 'A Release'
});
scope.updateName('a new name', release);
$httpBackend.flush();
expect(scope.isLoading).toBe(false); // fails
}));
更新:我尝试在最终期望()之前添加范围。$ digest(),就像在Why is my $q deferred not resolving in Angular unit test?中一样,但它在我的案例中没有用:(
答案 0 :(得分:3)
这就是我最终修复测试的方法:
it('$scope.updateName() should update release name', inject(function(ReleaseService) {
var data = {
_id: '456',
name: 'v1.0',
description: 'A Release'
};
var expectedResponse = {
_id: '525a8422f6d0f87f0e407a33',
name: 'a new name',
description: 'A Release about Agile Warlock'
};
// mock release object
var release = new ReleaseService(data);
scope.release = release;
scope.projectId = '123';
$httpBackend.expectPUT('projects/123/releases/456').respond(expectedResponse);
scope.updateName('a new name', release);
$httpBackend.flush();
expect(scope.isLoading).toBe(false);
}));