ngx-translate立即函数不是抛出的函数:单元测试

时间:2019-07-19 11:57:54

标签: angular typescript unit-testing karma-jasmine ngx-translate

对于单元测试和Angular,我还很陌生,我正在尝试为我的项目编写一些单元测试。

我得到的错误是;

ALTER TABLE public.foo ALTER COLUMN  bar SET DEFAULT NULL

component.spec文件的导入包括

Uncaught TypeError: _this.translate.instant is not a function thrown
Expected null to be truthy.

我了解到beforeEach(async(() => { TestBed.configureTestingModule({ imports:[ ClarityModule, RouterTestingModule, TranslateModule.forRoot({ loader: { provide: TranslateLoader, useFactory: (http: HttpClient) => new TranslateHttpLoader(http, 'assets/i18n/', '.json'), deps: [HttpClient] } }), HttpClientModule ], declarations: [ HeaderComponent, LanguageSelectorComponent ], providers: [ { provide: Store, useClass: TestStore }, { provide: TranslateService, useClass: TranslateServiceStub }, { provide: NGXLogger, useClass: NGXLoggerMock } ] }) .compileComponents(); })); 不包含“即时”功能。这是此错误的原因吗?如果是这样,我该如何模拟translate.instant函数?

或者在调用即时方法之前,有什么方法可以将翻译文件加载到规范文件中吗?

任何建议都会大有帮助。非常感谢!

1 个答案:

答案 0 :(得分:1)

根据documentation linkinstant的返回类型为stringObject

instant(key: string|Array<string>, interpolateParams?: Object): string|Object
export class TranslateServiceStub {
    instant(): string{
      // or put some logic to return Mock data as per the passed value from component
      //  (if being used at multiple places in the component)
      return "some_string";
    }
}

类似地,如果需要,您可以返回Object