模块“DynamicTestModule”导入了意外值“[object Object]”

oba*_*bar 5 testing angular

我遇到主题错误的问题。我正在使用 Angular 7 版本。这是我的测试:landing.component.spec.ts

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { LandingComponent } from './landing.component';
import { PegasPanelComponent } from '../pegas-panel/pegas-panel.component';
import { TranslateModule, TranslateService } from '@ngx-translate/core';
import { TranslateServiceStub } from 'src/app/app-tests.spec';
import { RouterModule } from '@angular/router';


describe('LandingComponent', () => {
  let component: LandingComponent;
  let fixture: ComponentFixture<LandingComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [LandingComponent, PegasPanelComponent, ],
      imports: [RouterModule, TranslateModule],
      providers: [{
        provide: TranslateService,
        useClass: TranslateServiceStub
      }]
    }).compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(LandingComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});
Run Code Online (Sandbox Code Playgroud)

登陆组件.ts

import { Component, OnInit } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';

@Component({
  selector: 'app-landing',
  templateUrl: './landing.component.html',
  styleUrls: ['./landing.component.scss']
})
export class LandingComponent implements OnInit {
  constructor(private translateService: TranslateService) { translateService.setDefaultLang('en'); }

  public ngOnInit() { }
}
Run Code Online (Sandbox Code Playgroud)

应用程序测试.spes.ts:

import { Observable, of } from 'rxjs';
import { EventEmitter } from '@angular/core';

export class TranslateServiceStub{

    public get(key: any): any {
        return of(key);
    }

  public setDefaultLang() {
    return of('en');
  }

    public onLangChange: EventEmitter<any> = new EventEmitter();
    public onTranslationChange: EventEmitter<any> = new EventEmitter(); 
    public onDefaultLangChange: EventEmitter<any> = new EventEmitter();
}
Run Code Online (Sandbox Code Playgroud)

和错误:

HeadlessChrome 0.0.0 (Windows 10 0.0.0) LandingComponent should create FAILED
        Failed: Unexpected value '[object Object]' imported by the module 'DynamicTestModule'
        Error: Unexpected value '[object Object]' imported by the module 'DynamicTestModule'
Run Code Online (Sandbox Code Playgroud)

我不知道如何解决它。尝试添加路由并将translateModule从声明移动到导入(我找到了两个答案),但这没有帮助。

Mic*_*ael 2

您的翻译模块存根的导入路径是绝对的而不是相对的,因此我会尝试更改它,具体取决于您的构建选项,这可能会导致问题。

另外,导入routes似乎是错误的 - 通常您会将它们作为参数提供给路由模块,但对于单元测试文件,您可能可以忽略它们

路由器测试模块应该像这样导入RouterTestingModule.withRoutes([])- 请参阅https://angular.io/api/router/testing/RouterTestingModule