Reputation: 7475
I'm trying to test my controller, when I'm running the test I get:
TypeError: Object # has no method '$watch'
In my controller I use $scope.$watch, how can I solve this issue?
controllerSpecs.js
describe('controllers', function(){
var scope, ctrl,timeout;
beforeEach(module('controllers'));
beforeEach(inject(function($controller) {
scope = {};
timeout = {};
ctrl = $controller('PublishersCtrl', {$scope:scope,APIService:APIService,$timeout:timeout});
}));
it('should have scope variable equals number', function() {
expect(scope.number).toBe(3);
});
});
controller.js:
controller('PublishersCtrl',['$scope','APIService','$timeout', function($scope,APIService,$timeout) {
$scope.number = 3;
/* Make sure second click on order will revert the sorting */
$scope.$watch('orderByField', function() {
$scope.reverseSort = true;
}); // initialize the watch
APIService.get_publisher_list().then(function(data){
$scope.render_table_and_filters(data);
});
}
Error:
TypeError: Object #<Object> has no method '$watch'
at new <anonymous> (C:/angular-client/app/js/controllers.js:24:12)
Upvotes: 2
Views: 1860
Reputation: 9614
describe('controllers', function(){
var scope, ctrl, timeout;
beforeEach(module('controllers'));
beforeEach(inject(function($rootScope, $controller) {
scope = $rootScope.$new(); // this is what you missed out
timeout = {};
controller = $controller('PublishersCtrl', {
$scope: scope,
APIService: APIService,
$timeout: timeout
});
}));
it('should have scope variable equals number', function() {
expect(scope.number).toBe(3);
});
});
Is your apps module called controllers
? What did you assign to your ng-app
?
Upvotes: 4