我有一个Angular 2应用程序,我使用karma和jasmin进行单元测试。 我在运行测试时遇到以下错误: "错误:没有令牌翻译的提供者!"
这是组件构造函数(它是应用程序组件):
constructor(private _ccmApiService: CcmApiService,
private _usersService: UsersService,
private _foldersService: FoldersService,
private _hubService: HubService,
private _router: Router,
private _navigationService: NavigationService,
private _translate: TranslateService,
private _busyLoaderService: BusyLoaderService,
private _infoModal: InfoModal) {
}
不需要提供类似于错误中的任何服务或模块。
有什么想法吗?
谢谢:)
答案 0 :(得分:0)
这可能是因为您注入的某个服务正在注入您的测试区域中未提供的其他服务(令牌转换)。你有没有记下所有这些注射?因为强烈建议当你对一个组件进行单元测试时,你会对注入进行存根,否则你就会一起测试所有这些注入。它会是这样的:
let someServiceStub= {...};
let otherServiceStub= {...};
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
...
],
declarations: [
...
],
providers: [
// override providers from other components and modules
{provide: SomeService, useValue: otherServiceStub},
]
})
// Override component's own provider
.overrideComponent(MyComponent, {
set: {
providers: [
{provide: OtherService, useValue: someServiceStub }
]
}
});
}
);