Argument' HeaderCtrl'不是一个功能,未定义

时间:2014-05-12 10:34:10

标签: angularjs angularjs-module angularjs-injector

我在AngularJS中测试我的控制器有一些问题,我收到以下错误:

enter PhantomJS 1.9.7 (Linux) Controller: HeaderCtrl Should redirect on a new user form FAILED
Error: Injector already created, can not register a module!
    at workFn (/path/ui/bower_components/angular-mocks/angular-mocks.js:2038)
Error: [ng:areq] Argument 'HeaderCtrl' is not a function, got undefined
http://errors.angularjs.org/1.3.0-beta.5/ng/areq?p0=HeaderCtrl&p1=not%20a%20function%2C%20got%20undefined
    at assertArg (/path/ui/bower_components/angular/angular.js:1443)
    at assertArgFn (/path/ui/bower_components/angular/angular.js:1454)
    at /path/ui/bower_components/angular/angular.js:7134
    at /path/test/ui/unit/core/controllers/header.js:37
    at invoke (/path/ui/bower_components/angular/angular.js:3873)
    at workFn (/path/ui/bower_components/angular-mocks/angular-mocks.js:2171)
undefined

这是我的测试:

describe('Controller: HeaderCtrl', function () {

    //<--------------------------------------------------------------------------->
    //-                Define the before/after class/test behaviour
    //<--------------------------------------------------------------------------->
    // load the controller's module
    beforeEach( module('MyApp') );

    var HeaderCtrl,
        authService,
        scope;

    // define the mock Auth service
    beforeEach( function() {
       authService = {
           auth: function() {}
       };
    });

    // Initialize the controller and a mock scope
    beforeEach( inject(function ($controller, $rootScope) {
        scope = $rootScope.$new();
        HeaderCtrl = $controller('HeaderCtrl', {
            $scope: scope,
            auth: authService
        });
    }));

    //<--------------------------------------------------------------------------->
    //-                             Tests
    //<--------------------------------------------------------------------------->
    describe('On a correct login for a new user', function() {
        it('Should redirect on a new user form', function () {

        });
    });
});

我的HeaderCtrl看起来像那样:

angular.module('MyApp')
.controller('HeaderCtrl', function ($scope, $http, $window, $interval, $location, error, core, auth) {

    $scope.user = auth.user;

    $scope.login = function () {
        auth.auth.query(function (data) {
           // doing some stuff
        });
    };

    $scope.logged = false;
});

此控制器正在使用某些服务,在其他模块中定义:

var services = angular.module('MyApp.services', ['ngResource']);

services.factory('auth', function ($resource, core) {
    var factory = {};

    factory.auth = $resource('/an/api/rest/service', {}, {
        query: {method: 'GET', params: {}, isArray: false}
    });

    factory.user = {};

    return factory;
});

根据要求定义了应用程序:

angular
    .module('MyApp', [
        'ngCookies',
        'ngResource',
        'ngSanitize',
        'ngRoute',
        'MyApp.services'
    ])
    .config(function ($locationProvider) {
        $locationProvider.html5Mode(false);
    })
    .config(function ($routeProvider) {
        $routeProvider
            .when('/', {
                templateUrl: 'views/core/main.html',
                controller: 'MainCtrl'
            })
            .otherwise({
                redirectTo: '/'
            });
    });

我在我的视图中使用我的HeaderCtrl direclty:

<div ng-controller="HeaderCtrl" ng-include="'views/core/fragments/header.html'"></div>

似乎我的控制器没有被业力加载,但是我检查了我的业力配置文件并且加载了所有.js文件......我甚至首先加载app.js以确保模块已创建:

files: [
        '../ui/bower_components/angular/angular.js',
        '../ui/bower_components/angular-mocks/angular-mocks.js',
        '../ui/bower_components/angular-resource/angular-resource.js',
        '../ui/bower_components/angular-cookies/angular-cookies.js',
        '../ui/bower_components/angular-sanitize/angular-sanitize.js',
        '../ui/bower_components/angular-route/angular-route.js',
        '../ui/scripts/app.js',
        '../ui/scripts/modules.js',
        '../ui/scripts/**/*.js',
        '../test/ui/**/*.js'
    ],

我错过了什么吗?我卡住了:'(

提前帮助你:)

yannig

2 个答案:

答案 0 :(得分:1)

我认为可能的问题是:

beforeEach( module('MyApp') );

但是应该通过

创建模块
beforeEach( module('MyApp', [ /* deps */ ]) );

答案 1 :(得分:1)

经过一段时间后,我在Google小组(https://groups.google.com/forum/#!topic/angular/wQWTJeLb0_U)的帮助下弄明白了。

我没有看到正确的地方,这是因为另一项测试(https://github.com/ychartois/HelpMeApp/blob/master/test/ui/unit/core/services/core.js)。我没有将测试封装在describe语句中。一旦我意识到这一点,就会出现真正的错误:我的karma.conf文件中缺少一个愚蠢的文件......

我不确定,但我想因为我的beforeEach不在describe声明中,模块的定义适用于我的所有测试,所以一旦我尝试在某个地方使用beforeEach( module('MyApp') );我收到错误:Injector already created, can not register a module!

我希望这个答案可以帮助别人;)