0

我在 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 直接:

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

似乎我的控制器没有被 karma 加载,但是我检查了我的 karma conf 文件并加载了所有 .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'
    ],

我错过了什么?我被困住了:'(

提前谢谢你的帮助:)

扬尼

4

2 回答 2

1

我认为可能的问题是:

beforeEach( module('MyApp') );

但是模块应该通过

beforeEach( module('MyApp', [ /* deps */ ]) );
于 2014-05-12T10:50:08.750 回答
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 文件中缺少的愚蠢文件......

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

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

于 2014-05-23T07:51:08.683 回答