翻訳機能がまだ機能するかどうかをテストするために、 $httpBackend と angular-translate がどのように連携するかを理解しようと多くの時間を費やしています。
私はこの時点で、この問題を解決する方法が本当にわかりません。
'use strict';
describe('Directive: translate', function () {
beforeEach(function () {
angular.module('myApp', ['pascalprecht.translate']);
});
var element,
$compile,
$rootScope,
$http,
$httpBackend;
beforeEach(inject(function (_$rootScope_, _$compile_, _$httpBackend_, _$http_) {
$compile = _$compile_;
$rootScope = _$rootScope_;
$http = _$http_;
$httpBackend = _$httpBackend_;
}));
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
it('should translate to English', function () {
element = $compile('<p translate>discover_more</p>')($rootScope);
$rootScope.$digest();
$httpBackend.expect('GET', 'langs/en.json').respond(200); // Should I return some data at this point?
$http.get('langs/en.json').then(function () {}); // Should I do something here?
$httpBackend.flush();
expect(element.html()).toBe('Discover more');
});
});
私のテストはもちろん失敗します。問題は、1) データを含む JSON を実際に取得する方法と、2) 「ここにデータがあります。作業を行ってください」という指示を言う方法がわからないということです。
編集:
わかりました、問題についていくつかの光。このAngularモジュールのテスト(https://github.com/angular-translate/angular-translate/tree/master/test/unit/directive)を見ていましたが、動作させることができました:
'use strict';
describe('Directive: translate', function () {
beforeEach(function () {
angular.module('gajoApp', ['pascalprecht.translate']);
});
var element,
$compile,
$rootScope;
beforeEach(module('pascalprecht.translate', function ($translateProvider) {
$translateProvider
.translations('en', {
'discover_more': 'Discover more'
})
.preferredLanguage('en');
}));
beforeEach(inject(function (_$rootScope_, _$compile_) {
$compile = _$compile_;
$rootScope = _$rootScope_;
}));
it('should translate to English', function () {
element = $compile('<p translate>discover_more</p>')($rootScope);
$rootScope.$digest();
expect(element.html()).toBe('Discover more');
});
});
しかし、私が望むのは、このソリューションを JSON を返す適切な AJAX 呼び出しと組み合わせて、これも行われたことをテストすることです。