VishwaKumar
VishwaKumar

Reputation: 3433

Karma-Jasmine Error

I have a following complex angularjs setup:

angular.module('a',['b','c'])
    .controller('c1', ['$scope', 'bservice', 'cservice', function($scope,bservice,cservice){

        // c1 controller code
    }]);

angular.module('b',['d','e'])
    .service('bservice', ['$scope', 'dservice', 'eservice', function($scope,dservice,eservice){

        // bservice code
    }]);

angular.module('c',['f','g'])
    .service('cservice', ['$scope', 'fservice', 'gservice', function($scope,fservice,gservice){

        // cservice code
    }]);

angular.module('d',[])
    .service('dservice', ['$scope', function($scope){

        // dservice code
    }]);

angular.module('e',[])
    .service('eservice', ['$scope', function($scope){

        // eservice code
    }]);

angular.module('f',[])
    .service('fservice', ['$scope', function($scope){

        // fservice code
    }]);

angular.module('g',[])
    .service('gservice', ['$scope', function($scope){

        // gservice code
    }]);

Now i am writing jasmine test case for the controller c1 , Here is my sample controller code

describe('c1 controller', function(){
        var scope, controller;
        var bservice, cservice;

        beforeEach( module('a') );

        beforeEach(inject(function(_$controller_, _$rootScope_, _bservice_, _cservice_) {
            scope = _$rootScope_.$new();

            controller = _$controller_('c1', {
                $scope: scope,
                bservice: _bservice_,
                cservice: _cservice_
            });

        }));

        it('scope should be defined', function() {
            expect(scope).toBeDefined();
        });

    });

But for some reason the test is failing and i am not getting any error message. The only message Firefox launcher shows is FAILED with following files:

minErr/<@ui-build/node_modules/angular/angular.js:68:12
loadModules/<@ui-build/node_modules/angular/angular.js:4779:15
forEach@ui-build/node_modules/angular/angular.js:357:11
loadModules@ui-build/node_modules/angular/angular.js:4740:5
createInjector@ui-build/node_modules/angular/angular.js:4662:19
WorkFn@ui-build/node_modules/angular-mocks/angular-mocks.js:3160:44
[3]</ContextKarma/this.loaded@http://localhost:9876/context.js:151:7

So my questions are:

Can this kind of angularjs setup is advisable for testing and if yes what am I doing wrong or missing that is causing the test to fail ?

How can i enable detail error log in karma ?

Upvotes: 0

Views: 346

Answers (1)

Gaurav
Gaurav

Reputation: 1233

The very first thing that you've done wrong is: The name of dependencies, as it should have been strings instead of variables.

So, instead of:

angular.module('a',['b','c']).controller('c1', [$scope, bservice, cservice, function($scope,bservice,cservice){
        // c1 controller code
    }]);

It should be:

angular.module('a',['b','c']).controller('c1', ['$scope', 'bservice', 'cservice', function($scope,bservice,cservice){
        // c1 controller code
    }]);

The next thing you should do is test each service and controller separately by mocking the required one during the individual service and controllers' test.

Here is the CodePen for the example. If, you'll mock the module and service properly it will work.

Upvotes: 1

Related Questions