如何模拟茉莉花因果中的翻译I18n?

问题描述 投票:0回答:1

所以我尝试使用Jasmine karma在组件级别进行一些单元测试。

并且我正在使用该翻译类:I18n

而且我有这个组成部分:

constructor(
    private qrCodeService: QRCodeDefinitionService,
    private i18n: I18n,
    private blockerService: ScreenBlockerService,
    route: ActivatedRoute,
    private router: Router,
    private fb: FormBuilder
  ) {
    const data = route.snapshot.data;
    this.echeqFamilies = data['echeqFamilies'];
    this.resources = data['resources'];
    this.definition = data.definition;
    this.qrcodeUsedForUpdate = this.definition.qrcode;
    this.qrcodeWithBaseUrl =  this.getFullQrUrl();
  }

这是它的规格:

describe('DefinitionComponent', () => {
  let component: DefinitionComponent;
  let fixture: ComponentFixture<DefinitionComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        QRCodeDefinitionModule,
        HttpClientTestingModule,
        TranslateModule.forRoot({

        })
      ],
      providers:[]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(DefinitionComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  fit('should create', () => {
    expect(component).toBeTruthy();
  });
});

但我收到此错误:

Error: StaticInjectorError(DynamicTestModule)[DefinitionComponent -> I18n]: 
  StaticInjectorError(Platform: core)[DefinitionComponent -> I18n]: 
    NullInjectorError: No provider for I18n!

那么如何解决这个问题?

谢谢

javascript angular unit-testing karma-jasmine angular-i18n
1个回答
0
投票

您需要通过在providers数组中添加I18n相关服务的提供程序来更新规格文件。您可以使用ng模块中使用的providers数组集,并在创建testBed时应用相同的集。]

© www.soinside.com 2019 - 2024. All rights reserved.