Question

I have some issue testing my controller in AngularJS, I got the following error:

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

Here is my test:

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 () {

        });
    });
});

And my HeaderCtrl look like that:

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;
});

This controller is using some services, defined in an other module like that:

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;
});

And as requested the app definition:

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: '/'
            });
    });

I use my HeaderCtrl direclty in my view like that:

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

It seems that my controller is not loaded by karma, but I check my karma conf file and all the .js files are loaded... I even load the app.js in first to be sure that the module is created:

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'
    ],

Did I miss something? I'm stuck :'(

Thx in advance for you help :)

yannig

Was it helpful?

Solution 2

I figure it out after a while and with the help of the Google group (https://groups.google.com/forum/#!topic/angular/wQWTJeLb0_U).

I didn't look at the right place, it was because of an other test (https://github.com/ychartois/HelpMeApp/blob/master/test/ui/unit/core/services/core.js). I didn't encapsulate the test in a describe statement. As soon as I realized that, the real error appear: it was a stupid file missing in my karma.conf file...

I'm not sure of it, but I suppose that because my beforeEach was not in a describe statement, the definition of the module was for all my tests, so as soon as I tried to use beforeEach( module('MyApp') ); somewhere I got the error: Injector already created, can not register a module!

I hope this answer can help someone else ;)

OTHER TIPS

I think possible problem is:

beforeEach( module('MyApp') );

But modules should be created via

beforeEach( module('MyApp', [ /* deps */ ]) );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top