mardi 29 septembre 2015

Getting Unknown provider in my unit test

I get the following error when trying to unit test my angular service.

Error: [$injector:unpr] Unknown provider: dbTranslateProvider.

I use Karma and Jasmine for testing and my code is packed with and WebPack. I'm new to Angular and Angular unit testing.

My unit test look like this (i'm not really testing anything yet):

describe("dbTranslate", function () {

    beforeEach(function () {
        var module = angular.module('ebcore');
    });

    beforeEach(inject(function ($injector) {

        var sut = $injector.get('dbTranslate');
        var test = $injector.get('$q');
    }));

    it("dbTranslate.translate", function () {
        $cacheFactory = function () { };
        $cacheFactory.get = function () { };
        //$dbTranslateCache = dbTranslate.dbTranslateCache($cacheFactory);
    });
});

The service i'm trying to test looks like this (typescript):

export class dbTranslate {
    static $inject = ['$q', '$http', 'dbTranslateCache'];

    constructor(private $q: ng.IQService, private $http: ng.IHttpService, private dbTranslateCache: ng.ICacheObject) {

    }

    ensure(texts: string[]) {
        var deferred = this.$q.defer();
        var missing = [];
        var values = {};

        texts.forEach(text => {
            var value = this.dbTranslateCache.get(text);
            if (typeof value === 'undefined') {
                missing.push(text);
            } else {
                values[text] = value;
            }
        });

        if (missing.length == 0) {
            deferred.resolve(values);
        } else {
            this.$http.post("/pub/texts/fetchText", missing).then(result => {
                for (var prop in result.data) {
                    if (result.data.hasOwnProperty(prop)) {
                        this.dbTranslateCache.put(prop, result.data[prop]);
                        values[prop] = result.data[prop];
                    }
                }
                deferred.resolve(values);
            }, error => {
                deferred.reject();
            });
        }

        return deferred.promise;
    }

    translate(text: string): string {
        return this.dbTranslateCache.get(text);
    }
}

If I try and debug my unit test I can see that the module._invokeQueue contains my service.

So any idea why can't I find my service through $injector? what am are missing?

Aucun commentaire:

Enregistrer un commentaire