【发布时间】:2021-01-10 16:55:16
【问题描述】:
运行 npm test 时出现错误
Can't bind to 'errorStateMatcher' since it isn't a known property of 'input'. ("dth">
<input matInput placeholder="Product Name" formControlName="prod_name"
[ERROR ->][errorStateMatcher]="matcher">
我的 Spec 文件如下
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { RouterTestingModule } from '@angular/router/testing';
import { ReactiveFormsModule } from '@angular/forms';
import { ProductAddComponent } from './product-add.component';
import { FormControl, FormGroupDirective, FormBuilder, FormGroup, NgForm, Validators } from '@angular/forms';
describe('ProductAddComponent', () => {
let component: ProductAddComponent;
let fixture: ComponentFixture<ProductAddComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [RouterTestingModule, ReactiveFormsModule],
declarations: [ ProductAddComponent ],
schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ProductAddComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
如何将errorStateMatcher 提供给测试单元?
【问题讨论】:
-
您可能需要将材料设计输入模块 (
MatInputModule) 导入您的测试平台,因为该属性与此相关。如果您想像使用CUSTOM_ELEMENTS_SCHEMA一样跳过这些类型的错误,我认为您也可以使用NO_ERRORS_SCHEMA架构。 -
@DanielWSrimpel 谢谢,这消除了错误
标签: angular-material karma-jasmine angular7