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)

Compare multiple input fields

I have following form:

enter image description here

I can add an answer with the add-button, so it is possible to create a question with more than two answers.

Also I have a pie chart of the results:

enter image description here

You can see my problem, do you? The piechart put my two answer options to one..

How can I check if one or more answers are the same?

4

1 回答 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);
    });
});

您的应用程序模块是否被调用controllers?你给你的分配了ng-app什么?

于 2013-12-29T17:03:11.743 回答