我有一个资源工厂
angular.module('mean.clusters').factory('Clusters', ['$resource',
function($resource) {
return $resource('clusters/:clusterId/:action', {
clusterId: '@_id'
}, {
update: {method: 'PUT'},
status: {method: 'GET', params: {action:'status'}}
});
}]);
和一个控制器
angular.module('mean.clusters').controller('ClustersController', ['$scope',
'$location', 'Clusters',
function ($scope, $location, Clusters) {
$scope.create = function () {
var cluster = new Clusters();
cluster.$save(function (response) {
$location.path('clusters/' + response._id);
});
};
$scope.update = function () {
var cluster = $scope.cluster;
cluster.$update(function () {
$location.path('clusters/' + cluster._id);
});
};
$scope.find = function () {
Clusters.query(function (clusters) {
$scope.clusters = clusters;
});
};
}]);
我正在编写我的单元测试,我发现的每个示例都使用某种形式$httpBackend.expect
模拟来自服务器的响应,我可以很好地做到这一点。
我的问题是,当对我的控制器功能进行单元测试时,我想模拟 Clusters 对象。如果我正在使用$httpBackend.expect
,并且我在我的工厂中引入了一个错误,我的控制器中的每个单元测试都会失败。
我想进行测试$scope.create
仅测试$scope.create
也不是我的工厂代码。
我尝试在以下位置添加提供商beforeEach(module('mean', function ($provide) {
我测试的一部分,但我似乎无法做对。
我也尝试过
clusterSpy = function (properties){
for(var k in properties)
this[k]=properties[k];
};
clusterSpy.$save = jasmine.createSpy().and.callFake(function (cb) {
cb({_id: '1'});
});
和设置Clusters = clusterSpy;
in the before(inject
但在创建函数中,间谍迷失了
错误:预期有间谍,但得到了功能。
我已经能够得到一个间谍对象来为cluster.$update
键入调用,但随后失败var cluster = new Clusters();
出现“不是函数”错误。
我可以创建一个适用于的函数var cluster = new Clusters();
但随后失败了cluster.$update
类型调用。
我可能在这里混合了术语,但是,有没有一种正确的方法可以用函数上的间谍来模拟集群,或者有一个很好的理由去使用$httpBackend.expect
?