Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit test when loading things at app run with AngularJS

I need my app to run some configuration at runtime vi an HTTP endpoint.

I wrote a simple service to do that:

module.factory('config', function ($http, analytics) {
    return {
        load: function () {
            $http.get('/config').then(function (response) {
                analytics.setAccount(response.googleAnalyticsAccount);
            });
        }
    }
});

Next, I call this module in a run block of my app module:

angular.module('app').***.run(function(config) {
        config.load();
    });

All is working well when the app is running but in my unit tests, I get this error: "Error: Unexpected request: GET /config"

I know what it means but I don't know how to mock it when it happens from a run block.

Thanks for your help

EDIT to add spec

Calling this before each

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

Tried this to mock $httpBackend:

beforeEach(inject(function($httpBackend) {

    $httpBackend.expectGET('/config').respond(200, {'googleAnalyticsAccount':});

    angular.mock.module('app')

    $httpBackend.flush();
}));

But got:

TypeError: Cannot read property 'stack' of null
        at workFn (/Users/arnaud/workspace/unishared-dredit/test/lib/angular/angular-mocks.js:1756:55)
    TypeError: Cannot read property 'stack' of null
        at workFn (/Users/arnaud/workspace/unishared-dredit/test/lib/angular/angular-mocks.js:1756:55)
    TypeError: Cannot read property 'stack' of null
        at workFn (/Users/arnaud/workspace/unishared-dredit/test/lib/angular/angular-mocks.js:1756:55)

EDIT since update to AngularJS 1.0.6

Since I've updated to AngularJS 1.0.6, advised by Igor from the Angular team, the issue is gone but now I've now got this one, which sounds more "normal" but I still can't figure out how to make it works.

Error: Injector already created, can not register a module!
like image 743
arnaud.breton Avatar asked Mar 26 '13 17:03

arnaud.breton


People also ask

Is AngularJS code unit testable?

AngularJS is written with testability in mind, but it still requires that you do the right thing. We tried to make the right thing easy, but if you ignore these guidelines you may end up with an untestable application.

What command is used to run unit tests to an Angular app?

If Angular CLI is used to manage the Angular projects, it will automatically support Jasmine and Karma Configurations. All you need in order to test your application is to type the command ng test.

How would you run your unit tests for an Angular project?

You can execute the unit tests for your app via the CLI by running ng test from within the root, project directory. Upon running ng test , two things happen. First, Angular uses the Karma test runner to open up a new browser window that contains the reporting mechanism for your unit tests.


1 Answers

I struggled with this error for a little while, but managed to come up with an sensible solution.

What I wanted to achieve is to successfully stub the Service and force a response, on controllers it was possible to use $httpBackend with a request stub or exception before initiating the controller. In app.run() when you load the module it initialises the object and it's connected Services etc.

I managed to stub the Service using the following example.

describe('Testing App Run', function () {
    beforeEach(module('plunker', function ($provide) {
        return $provide.decorator('config', function () {
            return {
                load: function () {
                    return {};
                }
            };
        });
    }));


    var $rootScope;
    beforeEach(inject(function (_$rootScope_) {
        return $rootScope = _$rootScope_;
    }));

    it("defines a value I previously could not test", function () {
        return expect($rootScope.value).toEqual('testing');
    });

});

I hope this helps your app.run() testing in the future.

like image 98
kylewelsby Avatar answered Oct 21 '22 13:10

kylewelsby