dimanche 31 mai 2015

Karma + Angular undefined error

I just started angular testing with karma and jasmine. I've written two basic tests. One of the two tests pass, but the other one is failing. I can't seem to debug it. I've been looking everywhere, and it should work according to various tutorials. The $scope variable should be available. I get the following error (and a lot of text, see screenshot):

           at C:/totalflatline-2.0/public/lib/angular/angular.js:4362
           at forEach (C:/totalflatline-2.0/public/lib/angular/angular.js:336)
           at loadModules (C:/totalflatline-2.0/public/lib/angular/angular.js:4364)
           at createInjector (C:/totalflatline-2.0/public/lib/angular/angular.js:4248)
           at workFn (C:/totalflatline-2.0/public/lib/angular-mocks/angular-mocks.js:2409)
           at C:/totalflatline-2.0/public/lib/angular-mocks/angular-mocks.js:2392
           at C:/totalflatline-2.0/public/shared/tests/unit/shared.client.controller.unit.tests.js:
5
           at C:/totalflatline-2.0/node_modules/karma-jasmine/lib/boot.js:126
           at C:/totalflatline-2.0/node_modules/karma-jasmine/lib/adapter.js:171
           at http://localhost:9876/karma.js:210
           at http://localhost:9876/context.html:83
       TypeError: 'undefined' is not an object (evaluating '$scope.test')
           at C:/totalflatline-2.0/public/shared/tests/unit/shared.client.controller.unit.tests.js:
9
           at C:/totalflatline-2.0/node_modules/karma-jasmine/lib/boot.js:126
           at C:/totalflatline-2.0/node_modules/karma-jasmine/lib/adapter.js:171
           at http://localhost:9876/karma.js:210
           at http://localhost:9876/context.html:83
hantomJS 1.9.8 (Windows 7): Executed 2 of 2 (1 FAILED) (0.402 secs / 0.025 secs)

The two tests are as following. The first one passes and the other one gives the above undefined error.

The one that passes:

describe('Testing MEAN Main Module', function() {
    var mainModule;
    beforeEach(function() {
        mainModule = angular.module('mean');
    });
    it('Should be registered', function() {
        expect(mainModule).toBeDefined();
        console.log('Success!');
    });
});

The one that fails:

describe('Testing the Shared Stats controller', function(){
    var SharedController,$scope;

    beforeEach(function(){
        // load the module you're testing.
        module('mean');

        inject(function($rootScope,$controller){
            // create a scope object for us to use.
            $scope = $rootScope.$new();

            SharedController = $controller('shared.StatsController',{
                $scope:$scope
            });
        });
    });
    it('Should contain a user object',function(){
        // User cannot be undefined
        expect($scope.test).toEqual('yoo');
    });

});

The angular controller is looking like this:

// Create the 'shared' controller
angular.module('shared').controller('shared.StatsController', [ '$scope',
    function($scope) {
        $scope.test = 'yoo';
    }
]);

Angular version is 1.4 and the karma dependencies are versions:

"karma": "~0.12.23",
"karma-jasmine": "~0.2.2",
"karma-phantomjs-launcher": "~0.1.4",

I have been breaking my neck over this all day. I hope someone with more knowledge about testing angular can help me out.

Aucun commentaire:

Enregistrer un commentaire