问题描述
如何测试使用 TranslocoLocale 管道(TranslocoDecimalPipe、TranslocoDatePipe 等)的服务?我显然可以自己模拟管道,但我不应该这样做。
我的组件模板中的 TranslocoLocale 管道在单元测试中正常工作。 在服务中使用管道,通过将它们注入构造函数,会导致单元测试中断。 它要么抱怨管道没有提供者,要么抱怨 ChangeDetectorRef 没有提供者。
在下面的示例中,我的服务使用 TranslocoDecimalPipe。在我的测试中,我收到错误:
NullInjectorError: StaticInjectorError(DynamicTestModule)[L10nTestService -> TranslocoDecimalPipe]:
StaticInjectorError(Platform: core)[L10nTestService -> TranslocoDecimalPipe]:
NullInjectorError: No provider for TranslocoDecimalPipe!
import { Injectable } from '@angular/core';
import { createServiceFactory,spectatorService } from '@ngneat/spectator/jest';
import { TranslocoModule } from '@ngneat/transloco';
import { TranslocoDecimalPipe,TranslocoLocaleModule } from '@ngneat/transloco-locale';
@Injectable({ providedIn: 'root' })
class L10nTestService {
decimalValue: string;
constructor(private translocoDecimalPipe: TranslocoDecimalPipe) {
this.decimalValue = translocoDecimalPipe.transform(1234567890.123456);
}
}
describe('L10ntestService',() => {
let spectator: spectatorService<L10nTestService>;
const createService = createServiceFactory<L10nTestService>({
service: L10nTestService,imports: [
TranslocoModule,TranslocoLocaleModule.init()
],providers: [TranslocoDecimalPipe]
});
beforeEach(() => spectator = createService());
it('decimal value should be transformed',() => {
expect(spectator.service.decimalValue).toBe('1,234,567,890.123');
});
});
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)