AngularJS test, can't find my controller

MathKimRobin

I met a problem with testing my controllers. I load them dynamically (lazy load, see after) so I wrote them like this :

angular.module('myApp').controllerProvider.register('DashboardCtrl', [
    '$scope', function ($scope) {
        $scope.foo = 'bar';
    }
]);

I initialise my module like this :

var app = angular.module('myApp', [
    'ngRoute',
    'ngCookies',
    'ngResource',
    'ngSanitize',
    'ui.router'
]);

app.run([
    '$rootScope', '$state', '$stateParams',
    function ($rootScope, $state, $stateParams) {
        $rootScope.$state = $state;
        $rootScope.$stateParams = $stateParams;
    }
]);

app.config(function ($stateProvider, $urlRouterProvider, $controllerProvider, $compileProvider, $filterProvider, $provide) {
    app.stateProvider = $stateProvider;
    app.routeProvider = $urlRouterProvider;
    app.controllerProvider = $controllerProvider;
    app.compileProvider = $compileProvider;
    app.filterProvider = $filterProvider;
    app.provide = $provide;

    $urlRouterProvider.otherwise('/');

    $stateProvider
        .state('dashboard', {
            url         : '/',
            templateUrl : 'views/dashboard.html',
            controller  : 'DashboardCtrl',
            resolve     : {
                deps : function ($q, $rootScope) {
                    var deferred = $q.defer();

                    curl('scripts/controllers/dashboard.js')
                        .then(function () {
                            $rootScope.$apply(function () {
                                deferred.resolve({});
                            });
                        });

                    return deferred.promise;
                }
            }
        });
});

That works. Method is described there : http://ify.io/lazy-loading-in-angularjs/

But when I want to test, I get each time the error : Error: [ng:areq] Argument 'DashboardCtrl' is not a function, got undefined

My test script :

describe('Controller: DashboardCtrl', function () {
    'use strict';

    var DashboardCtrl,
        scope;

    // load the controller's module
    beforeEach(function () {
        var app = angular.module('cellierApp', [
            'ngRoute',
            'ngCookies',
            'ngResource',
            'ngSanitize',
            'ui.router'
        ]);

        app.run([
            '$rootScope', '$state', '$stateParams',
            function ($rootScope, $state, $stateParams) {
                $rootScope.$state = $state;
                $rootScope.$stateParams = $stateParams;
            }
        ]);

        app.config(function ($stateProvider, $urlRouterProvider, $controllerProvider, $compileProvider, $filterProvider, $provide) {
            app.stateProvider = $stateProvider;
            app.routeProvider = $urlRouterProvider;
            app.controllerProvider = $controllerProvider;
            app.compileProvider = $compileProvider;
            app.filterProvider = $filterProvider;
            app.provide = $provide;
        });

        inject(function ($controller, $rootScope) {
            scope = $rootScope.$new();
            DashboardCtrl = $controller('DashboardCtrl', {
                $scope : scope
            });
        });
    });

    it('should attach a list of awesomeThings to the scope', function () {
        expect(scope.foo).toBeDefined();
        expect(scope.foo).toBe('bar');
    });
});

I don't know what I'm doing wrong. Is there someone who knows ? Thanks in advance

Nelson Yeung

I'm not sure whether Karma has support for Script.js loading, but for sure it has support for Require.js.

Since you followed that tutorial, have you checked out the final example he gave on github (It contains unit testing as well)? The idea is that you can convert everything to use Require.js instead of Script.js and use Karma with karma-requirejs for the testing.

Although it is not too difficult, there are quite a lot of changes. I won't post the working code here, but I would suggest reading the following: http://ify.io/unit-testing-lazily-loaded-angularjs-artefacts/ and http://karma-runner.github.io/0.8/plus/RequireJS.html

I can't link the final example because I don't have the reputation, but it's at the bottom of the tutorial you've linked.

To see a runnable example using Asynchronous Module Definitions with RequireJS, have a look at the sample app."

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

AngularJS can't find my controller

From Dev

Jasmine test for Angular can't find my controller

From Dev

can't access $routeParams in my angularjs controller

From Dev

angularjs - can not find the controller

From Dev

Angularjs is not able to find my controller

From Dev

AngularJS - Name of my ng-controller can't be found

From Dev

AngularJS karma jasmine test can't find route provider

From Dev

Why can't the NUnit Test Adapter find my FsUnit tests?

From Dev

Why can't the NUnit Test Adapter find my FsUnit tests?

From Dev

Can't initialise a controller in AngularJS

From Dev

Can't initialise a controller in AngularJS

From Dev

angularjs can't find "this" by jQuery?

From Dev

angularjs can't find "this" by jQuery?

From Dev

When testing angularjs controller - Can't find variable: module / inject by chutzpah

From Dev

Isolated Controller Test can't instantiate Pageable

From Dev

Can't get angular controller test to pass

From Dev

can't find my files

From Dev

can't find my files

From Dev

Can't find my error

From Dev

Can't find my trashcan?

From Dev

AngularJS can't call MVC controller

From Dev

AngularJS Can't inject resolved object on Controller

From Dev

Can't access form inside AngularJS controller

From Dev

Can't change 1 arg in controller in angularjs?

From Dev

Can't access code inside AngularJs controller

From Dev

Can't retrieve data from controller AngularJS

From Dev

Unit test can not find my methods/functions

From Dev

Why can my code not find the AngularJS $interval?

From Dev

Eclipse can't find logback-test.xml on my classpath for unit tests