首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何正确处理转储api中的单元测试标签

如何正确处理转储api中的单元测试标签
EN

Stack Overflow用户
提问于 2020-06-11 10:36:46
回答 1查看 2K关注 0票数 1

我使用Transloco已经有一段时间了,但是在单元测试期间我无法使用它。

我正在关注ngneat 用transloco进行单元测试的博客

但是我的简单测试用例也失败了,原因如下:

代码语言:javascript
复制
Chrome 83.0.4103 (Windows 10.0.0) AppComponent should render title in a h1 tag FAILED
        Error: Expected ' page-heading! ' to contain 'Welcome to transloco-sample!'.
            at <Jasmine>
            at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/src/app/app.component.spec.ts:40:54)
            at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:359:1)
            at ProxyZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:308:1)

我的配置文件和测试套件如下:

transloco-root.module.ts

代码语言:javascript
复制
import { HttpClient } from '@angular/common/http';
import {
  TRANSLOCO_LOADER,
  Translation,
  TranslocoLoader,
  TRANSLOCO_CONFIG,
  translocoConfig,
  TranslocoModule
} from '@ngneat/transloco';
import { Injectable, NgModule } from '@angular/core';
import { environment } from '../environments/environment';

@Injectable({ providedIn: 'root' })
export class TranslocoHttpLoader implements TranslocoLoader {
  constructor(private http: HttpClient) { }

  getTranslation(lang: string) {
    return this.http.get<Translation>(`/assets/i18n/${lang}.json`);
  }
}

@NgModule({
  exports: [TranslocoModule],
  providers: [
    {
      provide: TRANSLOCO_CONFIG,
      useValue: translocoConfig({
        availableLangs: ['fr', 'en'],
        defaultLang: 'en',
        // Remove this option if your application doesn't support changing language in runtime.
        reRenderOnLangChange: true,
        prodMode: environment.production,
      })
    },
    { provide: TRANSLOCO_LOADER, useClass: TranslocoHttpLoader }
  ]
})
export class TranslocoRootModule { }

app.component.html

代码语言:javascript
复制
<div style="text-align:center">
  <h1>
    {{ 'page-heading' | transloco }}!
  </h1>
</div>

transloco-testing.module.ts

代码语言:javascript
复制
import { TranslocoTestingModule, TranslocoConfig } from '@ngneat/transloco';
import * as en from '../assets/i18n/en.json';
import * as fr from '../assets/i18n/fr.json';

export function getTranslocoModule(config: Partial<TranslocoConfig> = {}) {
    return TranslocoTestingModule.withLangs(
        { en, fr },
        {
            availableLangs: ['en', 'fr'],
            defaultLang: 'en',
            ...config
        }
    );
}

测试套件

代码语言:javascript
复制
import { TestBed, async } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { AppComponent } from './app.component';
import { getTranslocoModule } from './transloco-testing.module';
import { LangDropDownComponent } from './lang-drop-down/lang-drop-down.component';

fdescribe('AppComponent', () => {

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        RouterTestingModule,
        getTranslocoModule()
      ],
      declarations: [
        AppComponent, LangDropDownComponent
      ],
    }).compileComponents();
  }));

  it('should create the app', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app).toBeTruthy();
  });

  it('should render title in a h1 tag', () => {
    const fixture = TestBed.createComponent(AppComponent);
    fixture.detectChanges();
    const compiled = fixture.debugElement.nativeElement;
    console.log(compiled.querySelector('h1').textContent);
    expect(compiled.querySelector('h1').textContent).toContain('Welcome to transloco-sample!');
  });
});

还有,en.json

代码语言:javascript
复制
{
    "application": "Transloco - Sample ",
    "title": "Testing Transloco",
    "para-title": "Here are some links to help you start: ",
    "page-heading": "Welcome to Transloco App",
    "client": {
        "name": "Name"
    }
}

在tsconfig.json中,我还有以下选项:

"resolveJsonModule":真,"esModuleInterop":真

有人能提点建议吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-24 09:49:04

张贴这个答案,这样可能会对其他人有所帮助。

根据transloco的文档,我们需要在tsconfig.json中添加以下参数:

代码语言:javascript
复制
{
  "resolveJsonModule": true,
  "esModuleInterop": true
}

在许多地方,有人写到需要在编译器选项中添加这些内容。但是它没有工作,所以我尝试将它们添加到angularCompilerOptions中的tsconfig.json中,它运行得很好。

因此,我将它们添加到我的tsconfig.json中如下:

代码语言:javascript
复制
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true,
    "resolveJsonModule": true,
    "esModuleInterop": true
  }

而且效果很好。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62322437

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档