변환 기능이 여전히 작동하는지 테스트하기 위해 $ 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) 지시어를 "여기에 데이터로 보내고 작업을 수행하는 방법"을 알 수 없다는 것입니다.
편집 :
좋아, 문제를 통해 어떤 빛. 에,
'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 호출이 솔루션을 결합이다 : 난 그냥이 각 모듈 (https://github.com/angular-translate/angular-translate/tree/master/test/unit/directive)의 시험을보고 있었고, 난 그것이 작동되도록 할 수 이것이 역시 행해졌다는 것을 시험하십시오.
나는 이미 명시 적으로 $ http 요청이 없어야한다고 생각했지만, 실제로하지 않으면 "No pending to request to flush!"라는 오류 메시지가 나타납니다. 나는 성공하지 못한 채 당신의 솔루션을 이미 시도해 보았습니다 :/각도 변환은 제대로 실행되기 전에 뭔가 다른 것을 필요로하는 것처럼 보입니다 ... 그러나 나는 그것이 무엇인지 알지 못합니다. 어쨌든 다니엘을 시도해 줘서 고마워. – DanielM
더 많은 정보를 얻으려고 ... 사실, $ translatorProvider (접두사와 기본 설정 언어 설정)를 통해 몇 가지 구성이 필요합니다. 테스트를 위해이 항목을 설정해야합니까, 아니면 앱 구성이 어떻게 든 나옵니까? 그리고 그럴 경우 어떻게하면 테스트 내부에서 .config()를 호출 할 수 있습니까? 또 다른 한가지는 beforEach()에서 $ convertate (첫 번째 구성에 대한 대안이 될 수 있음)를 주입하려고 할 때 컨트롤러를 사용하는 것처럼 "Unknown provider : $ translateProvider"오류가 발생한다는 것입니다. . – DanielM