【问题标题】:How to mock on a translation I18n in jasmine karma?如何在茉莉业力中模拟翻译 I18n?
【发布时间】:2019-11-12 11:25:20
【问题描述】:

所以我尝试使用 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!

那么如何解决这个问题?

谢谢

具有 I18 功能的翻译类:

import { InjectionToken, MissingTranslationStrategy } from "@angular/core";
export interface I18n {
    (def: string | I18nDef, params?: {
        [key: string]: any;
    }): string;
}
export interface I18nDef {
    value: string;
    id?: string;
    meaning?: string;
    description?: string;
}
export declare const MISSING_TRANSLATION_STRATEGY: InjectionToken<MissingTranslationStrategy>;
/**
 * A speculative polyfill to use i18n code translations
 */
export declare class I18n {
    constructor(format: string, translations: string, locale: string, missingTranslationStrategy?: MissingTranslationStrategy);
}

组件声明所在的模块:

@NgModule({
  declarations: [ListComponent, DefinitionComponent],
  imports: [
    // Angular
    CommonModule,
    FlexLayoutModule,
    FormsModule,
    ReactiveFormsModule,

    // Angular Material
    DragDropModule,
    MatButtonModule,
    MatCardModule,
    MatCheckboxModule,
    MatDialogModule,
    MatExpansionModule,
    MatFormFieldModule,
    MatIconModule,
    MatInputModule,
    MatSortModule,
    MatPaginatorModule,
    MatSelectModule,
    MatSnackBarModule,
    MatTabsModule,
    MatTableModule,
    MatTreeModule,
    MatDatepickerModule,
    MatNativeDateModule,
    AuthModule,

    // Carapax
    QRCodeDefinitionRoutingModule,
    SharedModule,
  ],
  providers: [
    MatDatepickerModule,
  ]
})
export class QRCodeDefinitionModule { }

我是这样的:


  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        QRCodeDefinitionModule,
        HttpClientTestingModule,
        TranslateModule.forRoot()
      ],
      providers:[]
    })
    .compileComponents();
  }));

但还是会出现这个错误:

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

【问题讨论】:

    标签: javascript angular unit-testing karma-jasmine angular-i18n


    【解决方案1】:

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

    【讨论】:

    • 能分享一下声明这个组件的模块文件和翻译类吗
    • 嗨,巴努,这就够了吗?
    • 我怀疑您应该有一个需要编写的翻译服务。应用程序翻译功能是否正常工作?你在使用像 ngx-translate 这样的包吗?
    • 我是一个包:“@ngx-translate/i18n-polyfill”:“^1.0.0”,在 package.json 中。是的,它正在工作
    • TranslateModule.forRoot({ }) 将其替换为 TranslateModule.forRoot() 并尝试
    猜你喜欢
    • 2020-03-27
    • 2014-12-12
    • 2015-09-19
    • 2017-01-15
    • 2017-05-26
    • 2020-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多