无法绑定到 'errorStateMatcher',因为它不是 'input' 的已知 属性

Can't bind to 'errorStateMatcher' since it isn't a known property of 'input'

我在 运行 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。

我必须将 matInput 属性添加到 input 元素。

如果您在测试时遇到此错误,请确保您也模拟了 matInput。那是我的问题。我在 spec 文件中导入了 MatInputModule,但后来导致了更多错误。