How do unit test with angular-translate

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

    it's a known issue, please follow the documentation here: unit testing angular

    The solution

    Unfortunately, this issue is caused by the design of angular-translate. To get around these errors, all we can do is to overwrite our module configuration in our test suite, that it doesn't use asynchronous loader at all. When there's no asynchronous loader, there's no XHR and therefore no error.

    So how do we overwrite our module configuration at runtime for our test suite? When instantiating an angular module, we can always apply a inline function which is executed as configuration function. This configuration function can be used to overwrite the modules configuration since we have access to all providers.

    Using the $provide provider, we can build a custom loader factory, which should then be used instead of the static files loader.

    beforeEach(module('myApp', function ($provide, $translateProvider) {
    
      $provide.factory('customLoader', function () {
        // loader logic goes here
      });
    
      $translateProvider.useLoader('customLoader');
    
    }));
    

    Please read more in the above link provided.

提交回复
热议问题