我正在使用 angular2 进行组件测试。在我的 html 模板中,我使用翻译管道。这是测试的代码:
import { ComponentFixture, TestBed ,getTestBed} from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';
import { RightComponent } from './right.component';
import {TranslateService} from 'ng2-translate/ng2-translate';
import {Injector} from "@angular/core";
let comp: RightComponent;
let fixture: ComponentFixture<RightComponent>;
let el: DebugElement;
let translate: TranslateService;
let injector: Injector;
describe('testComponent', () => {
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [ RightComponent ]
});
injector = getTestBed();
translate = injector.get(TranslateService);
fixture = TestBed.createComponent(RightComponent);
comp = fixture.componentInstance; // BannerComponent test instance
// get title DebugElement by element name
el = fixture.debugElement.query(By.css('h2'));
});
it('should display original title', () => {
fixture.detectChanges(); // trigger data binding
expect(el.nativeElement.textContent).toContain('Liste des droits');
});
});
我收到此错误,翻译管道未知:
Error: Template parse errors:
The pipe 'translate' could not be found ("<h2>[ERROR ->]{{'RIGHT_TITLE' | translate}}</h2>
<div class="table-responsive">
<table id="rightTableId" clas"): RightComponent@0:4
The pipe 'translate' could not be found ("
<table id="rightTableId" class="table table-striped">
<tr>
<th>[ERROR ->]{{'NAME_LABEL' | translate}}</th>
</tr>
<tr *ngFor="let right of rights">
"): RightComponent@4:16
The pipe 'translate' could not be found ("
</tr>
<tr *ngFor="let right of rights">
<td>[ERROR ->]{{right.name | translate}}</td>
</tr>
</table>
我们如何解决这个问题?
谢谢。