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

Tim*_*che 3 components unit-testing testbed angular-ngselect angular

我正在尝试使用 TestBed from 来测试在其 HTML 中具有 ng-select 的 Angular 组件。我首先收到错误消息,说无法绑定到“项目”,因为它不是“ng-select”的已知属性。所以我导入了 NgSelectModule 并将其添加到 TestBed 配置中的导入中。它现在返回Can't bind to '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);
    });
});
Run Code Online (Sandbox Code Playgroud)

小智 5

将表单模块导入您的测试台。

    TestBed.configureTestingModule({
        declarations: [ProductGenericGridComponent],
        imports: [HttpClientTestingModule, NgSelectModule, FormsModule],
        providers: []
    });
Run Code Online (Sandbox Code Playgroud)