如何测试与角控制器转换应用中的初始化配置? [英] How do I test controllers with Angular Translate initialized in App Config?

查看:132
本文介绍了如何测试与角控制器转换应用中的初始化配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用角转换一个应用程序(<一个href=\"https://github.com/Pascal$p$pcht/angular-translate\">https://github.com/Pascal$p$pcht/angular-translate).通过浏览器中翻译应用程序中的伟大的作品,但是当我尝试测试任何控制器,我得到的错误:意外的请求:GET区域/区域设置en.json 如何单元测试我控制器,因为翻译做了得到有关启动语言文件要求?

I have an app that uses Angular Translate (https://github.com/PascalPrecht/angular-translate). Translate works great in the application via browser but when I try to test any controller I get Error: Unexpected request: GET locale/locale-en.json. How do I unit test my controllers since translate does a GET request for the language file on startup?

我使用同噶自耕农角发生器。

I am using the yeoman angular generator with Karma.

App.js:

angular.module('myApp', ['ngCookies', 'ui.bootstrap', 'pascalprecht.translate'])
  .config(function ($routeProvider, $locationProvider, $translateProvider) {

    $routeProvider
      .when('/', {
        templateUrl: 'views/main.html',
        controller: 'MainCtrl'
      })
      .otherwise({
        redirectTo: '/'
      });

      $translateProvider.useStaticFilesLoader({
        prefix: 'locale/locale-',
        suffix: '.json'
      });
      $translateProvider.uses('en');
      $translateProvider.useLocalStorage();
  });

控制器测试:

describe('Controller: DocumentationCtrl', function () {

  // load the controller's module
  beforeEach(module('myApp'));

  var DocumentationCtrl,
    scope,
    $httpBackend;

  // Initialize the controller and a mock scope
  beforeEach(inject(function ($controller, $rootScope, $injector) {
    $httpBackend = $injector.get('$httpBackend');
    scope = $rootScope.$new();
    DocumentationCtrl = $controller('DocumentationCtrl', {
      $scope: scope
    });
  }));

  it('should attach a list of awesomeThings to the scope', function () {
    $httpBackend.whenGET('locale/locale-en.json').respond(200, {
      "TITLE": 'My App'
    });
    expect(scope.awesomeThings.length).toBe(3);
  });

});

文档控制器是一个标准产生的控制器。

The Documentation Controller is just a standard generated controller.

推荐答案

您必须指定配置阶段中的preferred语言而不是运行阶段。因此, $ translate.uses('我们')变成 $ translateProvider。preferredLanguage('我们')
同样适用于 useLocalStorage()。这些都是方法来配置 $转换服务。

You have to specify the preferred language within config phase rather then run phase. So $translate.uses('us') becomes $translateProvider.preferredLanguage('us'). Same goes for useLocalStorage(). These are all methods to configure $translate service.

您也应该尽量避免使用()来设置默认语言。使用 preferredLanguage()来代替。这样做的原因是, $ translate.uses()试图尽快加载一个国际化的文件,
如果有,它使用另一种语言的关键,使用()将加载两个文件,​​这是为什么我们推出 preferredLanguage()<一个cookie或类似/ code>键,是啊,这应该解决的问题。

You should also try to avoid uses() to set a default language. Use preferredLanguage() instead. The reason for this is, that $translate.uses() tries to load a i18n file as soon as possible, if there's a cookie or similar which uses another language key, uses() will load two files, that why we introduced preferredLanguage() And yea, this should solve the problem.

这篇关于如何测试与角控制器转换应用中的初始化配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆