如何以角度对 registerLocaleData 进行单元测试

Ron*_*onk 3 locale karma-runner angular

我正在尝试使用 angular 8 和 Karma 测试日期字符串转换的实际输出。

在我的 app.module 我使用语法: registerLocaleData(localeNl, 'nl');

在我的模板中,我使用 {{date | date: 'MMMM yyyy'}}

当我使用以下单元测试管道转换时:

element = fixture.nativeElement;
content = element.querySelector('.content');
expect(content.innerText).toContain('mei 2020')
Run Code Online (Sandbox Code Playgroud)

我收到 Karma 错误,即innerText 是“May 2020”而不是“mei 2020”,但在实际的角度应用程序中,它正确呈现文本“mei 2020”。那么如何让 Karma 环境使用与应用程序相同的语言环境呢?

Ali*_*F50 6

尝试提供它:

import { LOCALE_ID } from '@angular/core';
import { registerLocaleData } from '@angular/common';
...
registerLocaleData(localeNL, 'nl');
...
TestBed.configureTestingModule({
   providers: [{ provide: LOCALE_ID, useValue: 'nl' }], // you know the useValue here
});

Run Code Online (Sandbox Code Playgroud)

至于货币.不是,,我不知道为什么会这样。

  • 我还没有这样做,但也许`import localeNl from '@angular/common/locales/nl';`。https://gist.github.com/realappie/4e1f66e551f47549cd0306ad2e1fb0ef (2认同)