gpt4 book ai didi

angular - 错误 : Cannot create the component ComponentClass as it was not imported into the testing module

转载 作者:行者123 更新时间:2023-11-28 20:50:04 25 4
gpt4 key购买 nike

我在 Angular Cli 6.0.1 应用程序上设置了上述测试基础。

使用 Jasmine V2.8.0 和 Karma V2.0.0

我在第 13 行收到以下错误

错误:无法创建组件 AddressLookUpDirective,因为它没有导入到测试模块中!

无法弄清楚出了什么问题..欢迎提出任何建议

import { TestBed, ComponentFixture } from '@angular/core/testing';
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';

import { AddressLookUpDirective } from './address-look-up.directive';

describe('AddressLookUpDirective', () => {

let component: AddressLookUpDirective;
let fixture: ComponentFixture<AddressLookUpDirective>;

beforeEach(() => {

TestBed.configureTestingModule({
declarations: [
AddressLookUpDirective
]
});

fixture = TestBed.createComponent(AddressLookUpDirective);
component = fixture.componentInstance;

});
});

最佳答案

您应该创建一个模拟组件来测试指令并将指令和模拟组件导入 TestBed。

@Component({
template: `<input type="text" hoverfocus>`
})
class TestHoverFocusComponent {
}

@见https://codecraft.tv/courses/angular/unit-testing/directives/

关于angular - 错误 : Cannot create the component ComponentClass as it was not imported into the testing module,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50410292/

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