How do unit test with angular-translate

后端 未结 11 1944
死守一世寂寞
死守一世寂寞 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 10:03

    I wanted a solution,

    1. which was not too hacky
    2. which didn't require me to change my actual application code,
    3. which wouldn't interfere with the ability to load additional modules
    4. and most importantly which wouldn't require me to change every single test.

    This is what I ended up with:

    // you need to load the 3rd party module first
    beforeEach(module('pascalprecht.translate'));
    // overwrite useStaticFilesLoader to get rid of request to translation file
    beforeEach(module(function ($translateProvider) {
        $translateProvider.useStaticFilesLoader = function () {
        };
    }));
    

    Assuming you don't need the actual translations for your unit tests, this works great. Just put the beforeEach on a global level, preferably in it's own file inside the test folder. It will be executed before every other test then.

提交回复
热议问题