Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS factory testing in Karma with Jasmine

I am trying to test my AngularJS app with Jasmine via Karma. I get this error (at least, this is the latest of them):

Uncaught TypeError: Cannot read property '$modules' of null
    at /Users/benturner/Dropbox/Code/galapagus/app/static/js/angular-mocks.js:1866

From my karma.conf.js:

files: [
        'static/js/jquery.min.js',
        'static/js/angular.min.js',
        'static/js/angular-mocks.js',
        'static/js/angular-resource.min.js',
        'static/js/angular-scenario.js',
        'static/js/angular-loader.min.js',
        'static/js/momentous/ctrl_main.js',  // contains all my app's code
        'test/momentous.js'
],

Here is my test:

(function () {
    "use strict";

    var controller = null;
    var scope = null;

    describe("Services", inject(function($rootScope, Moments) {
        var mockedFactory, moments, flag, spy;
        moments = [{name: 'test'}];

        beforeEach(module('momentous', function($provide) {
            scope = $rootScope.$new();

            $provide.value('$rootScope', scope);

            mockedFactory = {
                getList: function() {
                    return moments;
                }
            };
            spy = jasmine.createSpy(mockedFactory.getList);

            $provide.value('Moments', mockedFactory);
        }));

        it('should return moments from the factory service', function() {
            runs(function() {
                console.log(scope.getList);
                flag = false;
                setTimeout(function() {
                    scope.getList();
                    flag = true;
                }, 500);
            });

            waitsFor(function() {
                return flag;
            }, "The call is done", 750);

            runs(function() {
                expect(scope.moments).toEqual([{name: 'test'}]);
                expect(spy).toHaveBeenCalled();
            });
        });
    }));

}());

So what I am trying to do is mock my factory service and check that it is returning an array of objects and setting them to a variable in $scope.

There's an async call in there too so I had to use runs() and waitsFor().

I still don't understand how I am injecting my $scope so that I can test against it, and with angular-mocks.js now giving me an error I feel I'm getting further away from solving this, not closer.

I cobbled this together from various docs, guides, and stackoverflow answers. Any guidance? Thanks.

like image 409
Xeus Avatar asked Sep 24 '13 09:09

Xeus


Video Answer


1 Answers

I'm also stuck getting this exact error. My code is similar where I am trying to test a provider so I call module and pass it a function that configures the provider.

SOLVED:

I found the issue is due to calling "inject" to return a delegate to the "describe" method. You can only use inject to return a delegate to "it".

For example:

describe('something', inject(function(something) {}));  // will throw the $module is null error

But this will work:

it('something', inject(function(something) {}));  // works :)
like image 148
JayPrime2012 Avatar answered Sep 28 '22 03:09

JayPrime2012