0

我正在使用 Jasmine/Karma 使用 Angular i18n 翻译(带有本地化)对代码进行单元测试。当前遇到以下错误:

AssetReplicateComponent > executable models > should have a title "Replicate Asset"
Expected ' _replicate_model_title ' to contain 'Replicate Asset'.

我正在设置测试如下:

  const translations = require('raw-loader!src/i18n/messages.en.xlf').default;
  beforeEach(waitForAsync(() => {
    class DummyModelDetailsComponent {}
...
    TestBed.configureTestingModule({

...
      providers: [
...
        
        { provide: TRANSLATIONS, useValue: translations },
        { provide: TRANSLATIONS_FORMAT, useValue: 'en' },
        { provide: LOCALE_ID, useValue: 'en' }
      ]
    })
    .compileComponents();

实际测试:

    it('should have a title "Replicate Asset"', () => {
      const title = fixture.debugElement.query(By.css('[data-cy=replicateModelTitle]'));

      expect(title.nativeElement.textContent).toContain('Replicate Asset');
    });

对应组件代码:

      <h4 data-cy="replicateModelTitle" class="d-inline-block text-secondary pr-3 mr-3 mb-2 border-right" i18n="@@replicate_model_title">
        _replicate_model_title
      </h4>

翻译文件:

      <trans-unit id="replicate_model_title" datatype="html">
        <source> _replicate_model_title </source>
        <context-group purpose="location">
          <context context-type="sourcefile">src/app/assets/components/asset-replicate/asset-replicate.component.html</context>
          <context context-type="linenumber">5,6</context>
        </context-group>
      </trans-unit>

单元测试时看起来根本没有翻译。我错过了什么?

4

0 回答 0