Anthony Joanes
Anthony Joanes

Reputation: 487

Unknown proider error with karma jasmine unit tests

I have a unit test that is created with the Jasmine framework. When I put a single test specification in the describe block I get a pass in the karma console. If I copy that describe block with the test in it ( the it(); ) then I suddenly start getting problems with dependencies that the module uses.

In the console I get errors around unknown providers.

Here is my simple test:

 describe('service definition tests', function () {

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

});

and that passes okay. If I copy this block I get an error about dependencies. Which is strange as I've already proved that I can test the 'sut' is defined in the first test.

One thing to note is that I have a beforeEach block that loads the module and provides a dependency and it is this dependency that errors when I've duplicated the test. Here is the beforeEach:

 var mockConfig = {};

 beforeEach(function () {
    module('app');
    module(function ($provide) {
        $provide.value('myConfig', mockConfig);
    });
});

the problem has to be something to do with this beforeEach being as the error I get is about the myConfig dependency.

Here is the error:

uncaught Error: [$injecor:unpr] Unknown provider: myConfigProvider <- myConfig <- authorisation
http://errors.angularjs.org/1.4.6/$injector/unpr?p0=myConfiProvider

Upvotes: 1

Views: 211

Answers (1)

Anthony Joanes
Anthony Joanes

Reputation: 487

I managed to resolve this issue by creating a dummy implementation of myConfig factory so that the test files used this.

angular.module('app').factory('myConfig', function() {

    var env = 'test';

    return {
        env: env
    }
});

This code lives in a js file that is loaded with the rest of the tests.

Upvotes: 0

Related Questions