【问题标题】:Can't bind to 'errorStateMatcher' since it isn't a known property of 'input'无法绑定到“errorStateMatcher”,因为它不是“输入”的已知属性
【发布时间】: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


【解决方案1】:

我认为你需要导入 MatInputModule。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2021-08-16
        • 2016-12-17
        • 2018-02-12
        • 1970-01-01
        • 1970-01-01
        • 2017-11-18
        • 2018-10-01
        • 1970-01-01
        相关资源
        最近更新 更多