2

THE SITUATION:

I am testing the function that allow the user to select a language. But I am getting the following error:

TypeError: $translate.use is not a function

I have seen similar questions in SO and other forums but they are not working for me.

THE CODE:

The configuration:

app.config(function($stateProvider, $urlRouterProvider, $translateProvider) {

$translateProvider.useSanitizeValueStrategy('sanitize');

$translateProvider.translations('EN', {

    "MY_ACCOUNT":                   "My account",
    "PROJECT_LIST":                 "Project List",
    "SETTINGS":                     "Settings",

});

$translateProvider.translations("IT", {

    "MY_ACCOUNT":                   "Mio account",
    "PROJECT_LIST":                 "Lista progetti",
    "SETTINGS":                     "Impostazioni",

});

$translateProvider.preferredLanguage(window.localStorage['language'] || 'EN');

$translateProvider.fallbackLanguage("EN");

The function:

$scope.select_language = function(language)
    {
        window.localStorage['language_code'] = language.code;

        $translate.use(language.code);

        $rootScope.app_language = language.code;
    }

The test:

describe('App tests', function() {

    beforeEach(module('my_app.controllers'));

    beforeEach(inject(function(_$controller_, _$rootScope_)
    {
        $controller = _$controller_;
        $rootScope = _$rootScope_;
        $scope = _$rootScope_.$new();   
        $translate = {};

        var controller = $controller('MainCtrl', { $scope: $scope, $rootScope: $rootScope, $translate: $translate });
    }));

    describe('Language tests', function() 
    {
        it('should properly load select_language function', function()
        {
            $scope.select_language({ name: "italiano", url: "img/italy.png", code: "IT"});
            expect($rootScope.app_language).toEqual("IT");
        });
    });

THE QUESTION:

Why I am receiving that error?

How can I properly test $translate.use?

FrancescoMussi
  • 20,760
  • 39
  • 126
  • 178

1 Answers1

4

I assume that the $translate is not your methods and you don't need to test it, then you can return an object with noop method

describe('App tests', function() {

beforeEach(module('my_app.controllers'));

beforeEach(inject(function(_$controller_, _$rootScope_)
{
    $controller = _$controller_;
    $rootScope = _$rootScope_;
    $scope = _$rootScope_.$new();   
    $translate = {
       use: function(){}
    };

    var controller = $controller('MainCtrl', { $scope: $scope, $rootScope: $rootScope, $translate: $translate });
}));

describe('Language tests', function() 
{
    it('should properly load select_language function', function()
    {
        $scope.select_language({ name: "italiano", url: "img/italy.png", code: "IT"});
        expect($rootScope.app_language).toEqual("IT");
    });
});

and if you need to checkout if it's called with correct parameters you can do

$translate = {
  use: jasmine.createSpy('$translate.use')
}

then you can add it to expectations

expect($translate.use).toHaveBeenCalledWith(//language code)
maurycy
  • 8,455
  • 1
  • 27
  • 44