错误:无法创建组件ComponentClass,因为它未导入测试模块

时间:2018-05-18 11:26:31

标签: angular unit-testing testing jasmine karma-runner

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

使用Jasmine V2.8.0和Karma V2.0.0

我在第13行收到以下错误

Error: Cannot create the component AddressLookUpDirective as it was not imported into the testing module!

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

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;

   });
});

1 个答案:

答案 0 :(得分:0)

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

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

@请参阅https://codecraft.tv/courses/angular/unit-testing/directives/