Angular TestBed:无法绑定到'ngModel',因为它不是'ng-select'的已知属性

时间:2018-03-20 12:02:45

标签: angular unit-testing components angular-ngselect testbed

我正在尝试使用TestBed来测试其HTML中具有ng-select的Angular组件。 我首先得到的错误是无法绑定到'items',因为它不是'ng-select'的已知属性。所以我导入了NgSelectModule并将其添加到了import在TestBed配置中。 它现在返回无法绑定到'ngModel',因为它不是'ng-select'的已知属性

import { getTestBed, TestBed } from '@angular/core/testing';
import { ProductGenericGridComponent } from './product-generic-grid.component';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { ProductGridService } from './product-generic-grid.service';
import { NgSelectModule } from '@ng-select/ng-select';
import { ProductItemComponent } from './../product-item/product-item.component';

describe('Product Generic Grid Component', () => {
    beforeEach(() => {
        TestBed.configureTestingModule({
            declarations: [ProductGenericGridComponent],
            imports: [HttpClientTestingModule, NgSelectModule],
            providers: []
        });
    });

    afterEach(() => {
        getTestBed().resetTestingModule();
    });

    it('should return true', () => {
        const fixture = TestBed.createComponent(ProductGenericGridComponent);
        expect(true).toBe(true);
    });
});

2 个答案:

答案 0 :(得分:4)

将表单模块导入测试平台。

    TestBed.configureTestingModule({
        declarations: [ProductGenericGridComponent],
        imports: [HttpClientTestingModule, NgSelectModule, FormsModule],
        providers: []
    });

答案 1 :(得分:0)

第一步:安装ng-select: npm install --save @ng-select/ng-select

第 2 步:导入 NgSelectModule 和 angular FormsModule 模块

从'@ng-select/ng-select'导入{NgSelectModule}; 从 '@angular/forms' 导入 { FormsModule };