2

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)
2
  • 1
    you are initializing scope to {}. So, $scope is {} and has not any angular method. Just remove the scope attribute in the beforeEach $controller() call.
    – Utopik
    Dec 29, 2013 at 16:33
  • @Utopik thanks, now i get different error, TypeError: Cannot set property 'number' of undefined i have in my controller $scope.number = 3, but now scope is undefined. Dec 29, 2013 at 16:42

1 Answer 1

4
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?

6
  • prefect it worked, but now i get an error, cause i'm calling another method from my original controller "TypeError: Object #<Object> has no method 'get_publisher_list'" Dec 29, 2013 at 17:11
  • i updated the controller code, can you help? how can i mouck up the functions inside my controller Dec 29, 2013 at 17:13
  • I think you should open another question for this as this has addressed the original one. Let's try not derail this answer, my be of help to someone else. Or we could move this to chat. @LiadLivnat
    – dcodesmith
    Dec 29, 2013 at 17:18
  • You need to test the service separately, I think. look at this docs.angularjs.org/guide/dev_guide.services.testing_services
    – dcodesmith
    Dec 29, 2013 at 17:58
  • please see other thread stackoverflow.com/questions/20828179/… Dec 29, 2013 at 18:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.