【发布时间】:2014-05-28 15:16:37
【问题描述】:
我有一个非常简单的控制器,它有一个调用服务方法来填充的范围变量。我正在尝试编写测试以确保填充此变量,但我收到错误 Expected spy find to have been called.
如何使用 spy 为服务块编写此单元测试?
describe('StuffCtrl', function() {
beforeEach(module('web'));
var scope, ctrl, stuff;
beforeEach(inject(function($rootScope, $controller, Stuff) {
scope = $rootScope.$new();
stuff = Stuff;
ctrl = $controller('StuffCtrl', {$scope: scope, Stuff: stuff});
}));
it('should fetch all the stuff', function() {
// Mock the Stuff.find service
spyOn(stuff, 'find');
// ERROR ON THIS LINE: Expected spy find to have been called.
expect(stuff.find).toHaveBeenCalled();
expect(scope.stuff).toBeDefined();
});
...
控制器
angular.module('web').controller('StuffCtrl',function($scope, $log, $routeParams, $location, Stuff){
$scope.stuff = Stuff.find();
});
东西服务
module.factory("Stuff", ['LoopBackResource', '$injector', function(Resource, $injector) {
var R = Resource(
urlBase + "/stuff/:id",
{ 'id': '@id' },
{"find": {
url: urlBase + "/stuffs",
method: "GET",
isArray: true,
}
});
return R;
}]);
【问题讨论】:
标签: angularjs unit-testing jasmine