How do unit test with angular-translate

后端 未结 11 1994
死守一世寂寞
死守一世寂寞 2020-12-08 09:38

I have uses angular translate from here (http://pascalprecht.github.io/angular-translate/) and it\'s just work fine, but it break my controller\'s unit test whith Error:

11条回答
  •  清歌不尽
    2020-12-08 09:54

    The 2016 answer for this is to preprocess your json into your tests and properly test translations work on your directives.

    I use karma-ng-json2js-preprocessor. Follow all the steps to setup your karma.conf then in your test file, prepend the relevant file as a module, then set that information in $translateProvider.

    beforeEach(module('myApp', '/l10n/english-translation.json'));
    
    // Mock translations for this template
    beforeEach(module(function($translateProvider, englishTranslation) {
        $translateProvider.translations('en_us', englishTranslation);
        $translateProvider.useSanitizeValueStrategy(null);
        $translateProvider.preferredLanguage('en_us');
    }));
    

    Note according to the plugin, it uses your filename to generate a camelcased module name. You can play with the function inside the module's /lib but basically it remove all dashes but KEEPS underscores in a camelCase. So en_us becomes En_us.

    You'll also need to tell your test that it is expecting that file as a GEt.

        $httpBackend.expect('GET', '/l10n/english-translation.json').respond(200);
    

提交回复
热议问题