参数'ListCtrl'不是函数,未定义

时间:2014-01-02 23:30:48

标签: angularjs jasmine karma-runner

我是Karma和Jasmine的新手,想要在控制器上使用grunt测试获得一些帮助。我运行测试时得到的错误是(缩短):

Running "karma:unit" (karma) task
INFO [karma]: Karma v0.10.9 server started at http://localhost:8080/
INFO [launcher]: Starting browser Chrome
WARN [watcher]: Pattern     "/Users/doronkatz/Development/Angular/Chapter4/test/mock/**/*.js" does not match any file.
INFO [Chrome 31.0.1650 (Mac OS X 10.9.1)]: Connected on socket 34BLydt6tI-UpBXZQBCZ
Chrome 31.0.1650 (Mac OS X 10.9.1) Controllers ListCtrl should have list of recipes FAILED
Error: [ng:areq] Argument 'ListCtrl' is not a function, got undefined
http://errors.angularjs.org/1.2.6/ng/areq?p0=ListCtrl&p1=not%20a%20function%2C%20got%20undefined
    at /Users/doronkatz/Development/Angular/Chapter4/app/bower_components/angular/angular.js:78:12
    at assertArg (/Users/doronkatz/Development/Angular/Chapter4/app/bower_components/angular/angular.js:1360:11)
    at assertArgFn (/Users/doronkatz/Development/Angular/Chapter4/app/bower_components/angular/angular.js:1370:3)
    at /Users/doronkatz/Development/Angular/Chapter4/app/bower_components/angular/angular.js:6755:9
    at null.<anonymous> (/Users/doronkatz/Development/Angular/Chapter4/test/spec/controllers/controllers.js:21:14)
    at Object.invoke (/Users/doronkatz/Development/Angular/Chapter4/app/bower_components/angular/angular.js:3697:17)
    at workFn (/Users/doronkatz/Development/Angular/Chapter4/app/bower_components/angular-mocks/angular-mocks.js:2102:20)
Error: Declaration Location
    at window.inject.angular.mock.inject (/Users/doronkatz/Development/Angular/Chapter4/app/bower_components/angular-mocks/angular-mocks.js:2087:25)
    at null.<anonymous> (/Users/doronkatz/Development/Angular/Chapter4/test/spec/controllers/controllers.js:17:16)
    at null.<anonymous> (/Users/doronkatz/Development/Angular/Chapter4/test/spec/controllers/controllers.js:13:3)
    at /Users/doronkatz/Development/Angular/Chapter4/test/spec/controllers/controllers.js:1:1
Expected undefined to equal [ 1, 2, 3 ].
Error: Expected undefined to equal [ 1, 2, 3 ].
    at null.<anonymous> (/Users/doronkatz/Development/Angular/Chapter4/test/spec/controllers/controllers.js:28:30)
Chrome 31.0.1650 (Mac OS X 10.9.1) Controllers EditController should save the recipe FAILED

...

Controllers.js test:

            describe('Controllers', function() {
              var $scope, ctrl;
              //you need to indicate your module in a test
              beforeEach(module('chapter4App',
                  ['directives', 'services']));
              beforeEach(function() {
                this.addMatchers({
                  toEqualData: function(expected) {
                    return angular.equals(this.actual, expected);
                  }
                });
              });

              describe('ListCtrl', function() {
                var mockBackend, recipe;
                // The _$httpBackend_ is the same as $httpBackend. Only written this way to
                // differentiate between injected variables and local variables
                beforeEach(inject(function($rootScope, $controller, _$httpBackend_, Recipe) {
                  recipe = Recipe;
                  mockBackend = _$httpBackend_;
                  $scope = $rootScope.$new();
                  ctrl = $controller('ListCtrl', {
                    $scope: $scope,
                    recipes: [1, 2, 3]
                  });
                }));

                it('should have list of recipes', function() {
                  expect($scope.recipes).toEqual([1, 2, 3]);
                });
              });

请查看包含代码的https://github.com/doronkatz/angularjs-book/tree/master/chapter4,包括控制器的测试脚本和控制器。

1 个答案:

答案 0 :(得分:0)

第4行:

警告[观察者]:模式“/Users/doronkatz/Development/Angular/Chapter4/test/mock/*/.js”与任何文件都不匹配。

Sooo,也许你没有加载所有必需的文件?