簡體   English   中英

使用Jasmine進行Angular5測試。 組件是2個模塊的聲明的一部分:AppModule和DynamicTestModule

[英]Angular5 Testing with Jasmine. Component is part of the declarations of 2 modules: AppModule and DynamicTestModule

我正在編寫第一個測試並處理此錯誤:

錯誤: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。 這會導致錯誤消息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM