mardi 6 février 2018

Problems testing a controller using Jasmine when the Module was defined in a different file

I am attempting to use Jasmine to test variables in the $scope of a controller whose module was defined in another file, however it doesn't seem to be working. Using this spec file:

describe('panels', function () {

        beforeEach(angular.mock.module('dashboard'));

        var $controller;

        beforeEach(angular.mock.inject(function(_$controller_){
          $controller = _$controller_;
        }));

        describe('test', function () {
                it('testing', function () {
                        var $scope = {};
                        var controller = $controller('panels_ctrl', { $scope: $scope });
                        expect($scope.consoleText).toBe(">");
                });
        });

});

I can access the scope of a file that looks like this:

angular.module('dashboard', []);
angular.module('dashboard')
        .controller('panels_ctrl', function ($scope){
            $scope.consoleText = ">"
        });

However when the first line (the one that defines the module) is in a different file I get the error

minErr/<@node_modules/angular/angular.js:116:12
loadModules/<@node_modules/angular/angular.js:5023:15
forEach@node_modules/angular/angular.js:408:11
loadModules@node_modules/angular/angular.js:4983:5
createInjector@node_modules/angular/angular.js:4900:19
WorkFn@node_modules/angular-mocks/angular-mocks.js:3173:44
[8]</ContextKarma/this.loaded@http://localhost:9876/context.js:1891:7
TypeError: $controller is not a function in cyphyint/apps/modules/dashboard/panels/panels.spec.js (line 14)
@cyphyint/apps/modules/dashboard/panels/panels.spec.js:14:21
[8]</ContextKarma/this.loaded@http://localhost:9876/context.js:1891:7

Which I've figured out means that the injector failed to access the controller.

For reference here is the files section of my karma.conf.js file (everything else is default):

files: [
    './node_modules/angular/angular.js',
    './node_modules/angular-*/angular-*.js',
    './cyphyint/apps/app.js',       
    './cyphyint/apps/modules/dashboard/dashboard_services.js',
    './cyphyint/apps/modules/dashboard/dashboard_ctrl.js',
    './cyphyint/apps/modules/dashboard/panels/panels_services.js',
    './cyphyint/apps/modules/dashboard/panels/panels.js',
    './cyphyint/apps/modules/dashboard/panels/panels.spec.js'
],

Does anyone know what the issue could be?

Aucun commentaire:

Enregistrer un commentaire