Angular TestBed:无法绑定到“ngModel”,因为它不是“ng-select”的已知属性
Posted
技术标签:
【中文标题】Angular TestBed:无法绑定到“ngModel”,因为它不是“ng-select”的已知属性【英文标题】:Angular TestBed: Can't bind to 'ngModel' since it isn't a known property of 'ng-select' 【发布时间】:2018-08-29 05:41:54 【问题描述】:我正在尝试使用 TestBed from 来测试一个在其 html 中具有 ng-select 的 Angular 组件。 我首先得到的错误是 Can't bind to 'items' because it is not an known property of 'ng-select'. 所以我导入了 NgSelectModule 并将其添加到导入在 TestBed 配置中。 它现在返回 Can't bind to 'ngModel' because it is not an known property of '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);
);
);
【问题讨论】:
【参考方案1】:将表单模块导入您的测试平台。
TestBed.configureTestingModule(
declarations: [ProductGenericGridComponent],
imports: [HttpClientTestingModule, NgSelectModule, FormsModule],
providers: []
);
【讨论】:
【参考方案2】:第 1 步:安装 ng-select: npm install --save @ng-select/ng-select
第 2 步:导入 NgSelectModule 和 angular FormsModule 模块
从'@ng-select/ng-select'导入 NgSelectModule ; 从“@angular/forms”导入 FormsModule ;
【讨论】:
以上是关于Angular TestBed:无法绑定到“ngModel”,因为它不是“ng-select”的已知属性的主要内容,如果未能解决你的问题,请参考以下文章
如何测试 angular 2 组件,其中嵌套组件具有自己的依赖项? (TestBed.configureTestingModule)
将 TypeMoq 模拟与 Angular TestBed 一起使用