0

Finally, I got to run test with jasmine and was succesfully all of my test with the standard mode.

I tried to change the way to declare my factory and service like a class with the constructor for each class and the necessary parameters. It is failing the test now.

What I don't understand I opened in browser my test and with Chrome I set my breakpoints. in the factory call, in service call. Finally in test I set breakpoints after the beforeEach and inside the inject. I set some breakpòints inside factory class and Chrom,e doesn't stop there.

When I execute the test the Location handler is returning undefined in lh variable, obiously, the call of functions inside factory fails.

My test is:

/// <reference path="../../../_references.js" />



describe("Factory - LocalizationHandler", function () {
    var lh;

    beforeEach(module('appCookIn.Factory'));

    beforeEach(function () {
        inject(function (LocalizationHandler, LocalizationService, $http, $q) {
            lh = LocalizationHandler;
        });
    });

    ///
    /// Testea que la funcion de GetConcept la devuelva coorectamente
    ///
    it("LocalizationHandler - GetConcept", function () {
        // Arrange
        var Expected = 'Nombre';
        var json = lh.SetNewCulture("es-ES");

        // Act
        var Result = lh.GetConcept(1);

        // Assert
        expect(Result).toBe(Expected);
    });

    ///
    /// Cambia la localización actual a otra.
    ///
    it("LocalizationHandler - SetNewCulture", function () {
        // Arrange
        var ExpectedCulture = "es-ES";
        var ExpectedResult = true;

        // Act

        var Result = lh.SetNewCulture("es-ES");
        var ResultCulture = lh.GetCurrentCulture();

        // Assert
        expect(ResultCulture).toBe(ExpectedCulture);
        expect(Result).toBe(ExpectedResult);

    });

    it("LocalizationHandler - GetCurrentCulture", function () {
        // Arrange
        var Expected = "es-ES";
        lh.SetNewCulture(Expected);

        // Act
        var Result = lh.GetCurrentCulture();

        // Assert
        expect(Result).toBe(Expected);
    });

});

My factory is:

LocalizationHandler = function (LocalizationService, $http, $q) {
    var self = this;

    self.LocalizationItems = null;
    self.culture = "es-ES";

    var res = {
        GetCurrentCulture: function ()
        {
            return self.culture;   
        },
        SetNewCulture: function (culture)
        {
            var data = JSON.parse(LocalizationService.SetNewCulture());

            self.LocalizationItems = data[culture];

            self.culture = culture;

            return self.LocalizationItems != null;
        },
        GetConcept: function (enumerable) 
        {
            if (self.LocalizationItems == null) return;
            return self.LocalizationItems[enumerable];
        }
    }

    self.LocalizationConstructor = function (res) {
        res.SetNewCulture(res.GetCurrentCulture());

    };

    self.LocalizationConstructor(res);

    return res;
 };

app.factory("LocalizationHandler", ["LocalizationService", "$http", "$q", LocalizationHandler]);

And my service is:

///
/// El servicio de localización trata el tema de la cultura para visualización de variables.
///


LocalizationService = function () {
    var self = this;

    self.LocalizationItems = null;
    self.culture = "es-ES";

    self.SetNewCulture = function () {
        var res = "";

        res += '{';
        res += '"es-ES": {';
        res += '"1": "Nombre",';
        res += '"2": "Llave de entrada"';
        res += '},';
        res += '"ca-ES": {';
        res += '"1": "Nom",';
        res += '"2": "Clau d`entrada"';
        res += '}';
        res += '}';

        return res;
    };

    self.LocalizationServiceCtor = function () {
    }

    self.LocalizationServiceCtor();
}

app.controller("LocalizationService", [LocalizationService]);
Dave
  • 7,028
  • 11
  • 35
  • 58
  • I can't really see what you're trying to achieve. Your trying to use your LocalizationService as controller (app.controller("Local....)) – m.brand Aug 25 '15 at 09:30

1 Answers1

1

I think your problem is that you're trying to register your "service" as a controller at the bottom of your LocalizationService.

try and use

app.service("LocalizationService", [LocalizationService]);

instead of

app.controller("LocalizationService", [LocalizationService]);

But in the end in don't really understand why you want to use a service and a factory in this example. Maybe check out:

Confused about Service vs Factory

Community
  • 1
  • 1
m.brand
  • 658
  • 5
  • 12
  • yes I forgot service and I forgot appName it was really AppCookIn and I had appCookIn. grrr jasmine didn't describe correctly the error. – Dave Aug 25 '15 at 10:49