Jak test jednostkowy z translacją kątową

Używam tutaj tłumaczenia kątowego (http://pascalprecht.github.io/angular-translate/) i po prostu działa dobrze, ale łamie test jednostki kontrolera kontrolera z błędem:

Unexpected request: GET scripts/i18n/locale-en.json

Nie rozumiem dlaczego?

Używam yeomana i testuję karmę.

app.js:

'use strict';

(function() {

  angular.module('wbApp', ['authService', 'authUserService', 'checkUserDirective', 'ui.bootstrap', 'pascalprecht.translate'])
    .config(function($routeProvider) {
      $routeProvider
        .when('/', {
          templateUrl: 'views/login.html',
          controller: 'LoginCtrl',
          access: {
            isFree: true
          }
        })
        .when('/main', {
          templateUrl: 'views/main.html',
          controller: 'MainCtrl',
          access: {
            isFree: false
          }
        })
        .otherwise({
          redirectTo: '/'
        });
    });

})();

configTranslate.js:

'use strict';

(function() {

  angular.module('wbApp')
    .config(['$translateProvider',
      function($translateProvider) {

        $translateProvider.useStaticFilesLoader({
            prefix: 'scripts/i18n/locale-',
            suffix: '.json'
        });

        $translateProvider.preferredLanguage('en');

      }]);

})();

karma.conf.js:

files = [

  ...

  'app/bower_components/angular-translate/angular-translate.js',
  'app/bower_components/angular-translate-loader-static-files/angular-translate-loader-static-files.js',

  ...

];

test kontrolera:

'use strict';

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

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

  var LoginCtrl, scope, location, httpMock, authUser;

  // Initialize the controller and a mock scope
  beforeEach(inject(function($controller, $rootScope, $location, $httpBackend, AuthUser) {
    authUser = AuthUser;
    location = $location;
    httpMock = $httpBackend;
    scope = $rootScope.$new();

    LoginCtrl = $controller('LoginCtrl', {
      $scope: scope
    });


    httpMock.when('GET', 'scripts/i18n/locale-en.json').passThrough();

  }));

  it(...);

  ...

});

jeśli dodam to do kontrolera testowego, ten sam błąd produktu:

httpMock.when('GET', 'scripts/i18n/locale-en.json').respond(200);
httpMock.flush();

lub

httpMock.when('GET', 'scripts/i18n/locale-en.json').passThrough();
httpMock.flush();

znajduję ten postJak przetestować kontrolery za pomocą Angular Translate zainicjowanego w App Config? ale nie pomógł mi: /

W swoich testach intensywnie używam $ httpBackend i działa dobrze, ale w tym przypadku jest nieskuteczny. Jeśli skomentuję linię:

$translateProvider.preferredLanguage('en');

oczywiście błąd, jeśli dodam środowisko wykonawcze (w moich kontrolerach)

$translate.uses(local);

Skończę z tym samym błędem?

Dlatego zwracam się do konfiguracji tłumaczenia (configTranslate.js) lub w czasie wykonywania jest taki sam wynik:

Unexpected request: GET scripts/i18n/locale-en.json

Oto składnia, którą testowałem, albo w „beforeEach (inject (function (...));”

lub w teście „to („ ... ”, funkcja () {...});”

httpMock.expectGET('scripts/i18n/locale-en.json');
httpMock.when('GET', 'scripts/i18n/locale-en.json').passThrough();
httpMock.when('GET', 'scripts/i18n/locale-en.json').respond(data);

z końcem

httpMock.flush();

Próbowałem również zastosować $

httpMock.expectGET('scripts/i18n/locale-fr.json');
scope.$apply(function(){
  $translate.uses('fr');
});
httpMock.flush();

nic się nie dzieje, ale ten błąd doprowadza mnie do szału ...

Jeśli masz jakieś sugestie

questionAnswers(11)

yourAnswerToTheQuestion