angular - 无法测试使用翻译管道的组件(找不到管道“翻译”)
问题描述
我有一个使用 ngx-translate 进行本地化的组件。创建模块的基本单元测试不起作用。它失败并出现以下错误:
PatientDataComponent should create FAILED
[ERROR ->]{{ 'patient.information' | translate }}
</p>
The pipe 'translate' could not be found ("<p>[ERROR ->]{{ 'patient.information' | translate }}</p>"): ng:///DynamicTestModule/PatientDataComponent.html@1:2
error properties: Object({ ngSyntaxError: true, ngParseErrors: [ The pipe 'translate' could not be found ("<p>"): ng:///DynamicTestModule/PatientDataComponent.html@1:2 ] })
该应用程序能够切换语言;因此 ngx-translate 似乎有效。除了在我的单元测试中。测试代码如下所示:
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { PatientDataComponent } from './patient-data.component';
describe('PatientDataComponent', () => {
let component: PatientDataComponent;
let fixture: ComponentFixture<PatientDataComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [PatientDataComponent]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(PatientDataComponent);
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
我需要做些什么来解决此错误并使组件可测试?
解决方案
在您的 testBed 配置中添加您在 appModule 或声明组件的模块中所做imports
的类似操作。TranslateModule
例如:
TestBed.configureTestingModule({
declarations: [PatientDataComponent],
imports: [
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: I18nHttpLoaderFactory,
deps: [HttpClient],
},
}),
],
}).compileComponents();
也许你有其他翻译配置。
推荐阅读
- octobercms - 在 octobercms 中使用 ajaxConfirmMessage 创建自定义确认对话框
- javascript - 在 localhost / 无 node.js 上使用 JavaScript 进行半动态导入或读取文件
- javascript - 如何过滤 API?
- kdb - 将符号作为参数传递给 kdb 中 fby 的函数时出错
- javascript - 在这堂课的最后一个“过去-2”之后,我将如何检查通过了多少堂课?
- javascript - Vue.js 中的 Spotify
- reactjs - 如何模拟 React setState 进行 API 测试
- swift - 如何在新根目录下将文本字段添加到 Firebase?
- visual-c++ - 为什么我的 lp 文件为每个变量提供#number?
- python - 过滤以特定字符串开头的 txt 行 (Python)