gpt4 book ai didi

angular - 模块 '[object Object]' 导入的意外值 'DynamicTestModule'

转载 作者:行者123 更新时间:2023-11-28 20:35:23 25 4
gpt4 key购买 nike

我对主题错误有疑问。我正在使用 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();
});
});

登陆.component.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() { }
}

应用测试.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();
}

和错误:

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'

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

最佳答案

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

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

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

关于angular - 模块 '[object Object]' 导入的意外值 'DynamicTestModule',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56035804/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com