无法绑定到“ errorStateMatcher”,因为它不是“输入”的已知属性

时间:2018-11-16 14:27:15

标签: angular-material karma-jasmine angular7

运行npm测试时出现错误

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

3 个答案:

答案 0 :(得分:0)

我认为您需要导入MatInputModule。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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