gpt4 book ai didi

使用 Jasmine 进行 Angular5 测试。组件是 2 个模块 : AppModule and DynamicTestModule 声明的一部分

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

我正在编写第一个测试并处理这个错误:

Error: Type SearchComponent is part of the declarations of 2 modules: AppModule and DynamicTestModule! Please consider moving SearchComponent to a higher module that imports AppModule and DynamicTestModule. You can also create a new NgModule that exports and includes SearchComponent then import that NgModule in AppModule and DynamicTestModule.

在我的应用程序中,我只有一个模块:AppModule.tsSearchComponent 只在那里声明。如果我创建第二个模块(又名 ChildModule)并将 SearchComponent 移到那里,错误消息将具有相同的内容,但模块的名称将从 AppModule 更改为 ChildModule。我的每个组件都有同样的错误。

如果我从规范文件中的导入中删除 AppModule,并添加 NO_ERRORS_SCHEMA,错误就会消失。

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { CommonModule } from '@angular/common';
import { HttpClientModule } from '@angular/common/http';
import { AppComponent } from './app.component';
import { OtherComponentOne} from './tools/features/other-component-one.component';
import { OtherComponentTwo} from './tools/features/other-component-two.component';
import {routing} from "./app.routes";
import { environment } from '../environments/environment';
import {SearchComponent} from "./tools/features/search/search.component";
import {FilterPipe} from "./tools/shared/filter-pipe/filter.pipe";
import { SomeDirective } from './tools/shared/directives/some.directive';
import {ServiceOne} from "./tools/services/service-one.service";
import {ServiceTwo} from "./tools/services/service-two.service";

@NgModule({
declarations: [
FilterPipe,
AppComponent,
OtherComponentOne,
OtherComponentTwo,
SearchComponent,
SomeDirective
],
imports: [
routing,
HttpClientModule,
BrowserModule,
FormsModule,
CommonModule,
ReactiveFormsModule
],
exports: [SomeDirective ],
providers: [ServiceOne, ServiceTwo],
bootstrap: [AppComponent]
})
export class AppModule {}

搜索.component.spec.ts

import {TestBed, async, ComponentFixture} from '@angular/core/testing';
import { SearchComponent } from './search.component';
import { ServiceOne} from '../../services/service-one';
import { ServiceTwo} from '../../services/service-two';
import { FilterPipe } from '../../shared/filter-pipe/filter.pipe';
import {AppModule} from "../../../app.module";

describe('Search Component', () => {
let component: SearchComponent;
let fixture: ComponentFixture<SearchComponent>;

beforeEach(() => {
TestBed.configureTestingModule({
declarations: [
SearchComponent, FilterPipe
],
imports: [AppModule], <----If I have NO_ERRORS_SCHEMA here instead of AppModule, test passes
providers: [ServiceOne, ServiceTwo]
});
fixture = TestBed.createComponent(SearchComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should be initialized', async(() => {
expect(component).toBeTruthy();
}));
});

最佳答案

请从您的 Search.component.spec.ts 中删除 AppModule

您使用来自 TestBed 的 TestingModule (DynamicTestModule) 进行测试。显然,您在配置 TestingModule 时声明了 SearchComponent,同时您导入了 AppModule,您还声明了 SearchComponent。这会导致错误消息。

关于使用 Jasmine 进行 Angular5 测试。组件是 2 个模块 : AppModule and DynamicTestModule 声明的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50398569/

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