gpt4 book ai didi

angular - 失败 : Unexpected directive 'ContactDetailsComponent' imported by the module 'DynamicTestModule' . 请添加@NgModule 注解

转载 作者:太空狗 更新时间:2023-10-29 17:19:02 26 4
gpt4 key购买 nike

我创建了一个新组件并进行了 ng 测试,但失败并出现以下错误

Failed: Unexpected directive 'ContactDetailsComponent' imported by the module 'DynamicTestModule'. Please add a @NgModule annotation.

import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { AdditionalContactDetailsComponent } from './additional-contact-details.component';
import { EdlInputModule, EdlIconModule, EdlMessagesModule } from '@fedex/ddt';
import { ReactiveFormsModule, FormBuilder, FormsModule } from '@angular/forms';
import { ContactDetailsComponent } from '../contact-details/contact-details.component';
import { HttpClientModule } from '@angular/common/http';
import { HttpClientTestingModule } from '@angular/common/http/testing';

fdescribe('AdditionalContactDetailsComponent', () => {
let component: AdditionalContactDetailsComponent;
let fixture: ComponentFixture<AdditionalContactDetailsComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [EdlInputModule,
ReactiveFormsModule,
FormsModule,
EdlIconModule,
EdlMessagesModule,
ContactDetailsComponent,
HttpClientModule,
HttpClientTestingModule],
declarations: [AdditionalContactDetailsComponent],
providers: [FormBuilder]
})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(AdditionalContactDetailsComponent);
component = fixture.componentInstance;
component.ngOnInit();
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});

最佳答案

从导入数组中删除 ContactDetailsComponent 组件并将其添加到声明数组中。 组件 始终放置在声明数组中,模块 放置在导入数组中。

解决方法如下:

import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { AdditionalContactDetailsComponent } from './additional-contact-details.component';
import { EdlInputModule, EdlIconModule, EdlMessagesModule } from '@fedex/ddt';
import { ReactiveFormsModule, FormBuilder, FormsModule } from '@angular/forms';
import { ContactDetailsComponent } from '../contact-details/contact-details.component';
import { HttpClientModule } from '@angular/common/http';
import { HttpClientTestingModule } from '@angular/common/http/testing';

fdescribe('AdditionalContactDetailsComponent', () => {
let component: AdditionalContactDetailsComponent;
let fixture: ComponentFixture<AdditionalContactDetailsComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [EdlInputModule,
ReactiveFormsModule,
FormsModule,
EdlIconModule,
EdlMessagesModule,
HttpClientModule,
HttpClientTestingModule],
declarations: [AdditionalContactDetailsComponent, ContactDetailsComponent],
providers: [FormBuilder]
})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(AdditionalContactDetailsComponent);
component = fixture.componentInstance;
component.ngOnInit();
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});

关于angular - 失败 : Unexpected directive 'ContactDetailsComponent' imported by the module 'DynamicTestModule' . 请添加@NgModule 注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54759915/

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