我在为翻译服务创建单元测试时遇到问题。我将此作为 TranslateService
的构造函数constructor(@Inject(TRANSLATIONS) private _translations: any) {}
我用
创建了 my translate.service.spec.tsdescribe('On initialize', () => {
it('No changes made to the service', async(inject([TranslateService], (service: TranslateService) => {
let translate = TestBed.get(TranslateService);
})));
});
但是,控制台提示我输入错误消息“没有TranslateService的提供程序”。如何在spec文件中注入@Inject?
答案 0 :(得分:0)
如果您使用此
TestBed.configureTestingModule({
providers: [
{ provide: TRANSLATION, useClass: TranslationService }
]
});
然后你需要这样做
it('should inject', inject([TRANSLATION], (value: TranslationService) => {
}));
provide
指定我们可以注入的令牌。由于令牌不是TranslationService
,我们无法注入TranslationService
。我们需要通过令牌TRANSLATION
注入是否在TranslationService
class TransactionService {
constructor(@Inject(TRANSLATION) value) {}
}
然后你应该做更像
的事情TestBed.configureTestingModule({
providers: [
TranslationsService,
{ provide: TRANSLATION, useValue: whateverTranslationIs }
]
});
it('should inject', inject([TranslationService], (value: TranslationService) => {
}));
您需要在测试台配置中配置TRANSACTIONS
注射剂