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


  错误:SearchComponent类型是2个模块的声明的一部分:AppModule和DynamicTestModule!请考虑将SearchComponent移至导入AppModule和DynamicTestModule的更高模块。您还可以创建一个新的NgModule,该导出并包含SearchComponent,然后在AppModule和DynamicTestModule中导入该NgModule。


在我的应用程序中,我只有一个模块:AppModule.ts。并且SearchComponent仅在此处声明。如果我创建第二个模块(又名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 {}


Search.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。这会导致错误消息。

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

10-12 04:51