sách gpt4 ai đã đi

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

In lại 作者:行者123 更新时间:2023-11-28 20:35:23 25 4
mua khóa gpt4 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;

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';

@Thành phần({
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 = new EventEmitter();
public onTranslationChange: EventEmitter = new EventEmitter();
public onDefaultLangChange: EventEmitter = 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 从声明移动到导入(我找到了两个答案)但这没有帮助。

1 Câu trả lời

翻译模块 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
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com