Ng测试-无法绑定到“值”,因为它不是“ mat-select”的已知属性

时间:2018-08-10 17:43:29

标签: testing angular-material angular-cli angular6

我有一个使用Material Design的Angular6应用程序。运行“ ng测试”时,出现以下错误:

  

失败:模板解析错误:由于它不是'mat-select'的已知属性,因此无法绑定到'value'。

我在进出口中都包含了MatSelectModule。它在应用程序中工作正常,但在测试过程中失败。

material-design.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { MatFormFieldModule, MatInputModule, MatSelectModule} from '@angular/material';

@NgModule({
  imports: [MatFormFieldModule, MatInputModule, MatSelectModule],
  exports: [MatFormFieldModule, MatInputModule, MatSelectModule],
})
export class MaterialDesignModule { }

选择示例:

<mat-form-field class='select_buttons'>
  <mat-select (selectionChange)='typeSelection_changed($event)' [(value)]='type'>
    <mat-option *ngFor="let type of types" [value]="type"> {{type.name}}</mat-option>
  </mat-select>
</mat-form-field>

2 个答案:

答案 0 :(得分:1)

您是否要在spec.ts文件中导入材料模块?

beforeEach(async(() => {
  TestBed.configureTestingModule({
    imports: [ MaterialDesignModule ],
    declarations: [ FooComponent ]
  })
  .compileComponents();
}));

答案 1 :(得分:0)

您必须从import { MatSelectModule } from '@angular/material';导入MatSelectModule 并将其添加到您的规格导入中

beforeEach(async(() => {
  TestBed.configureTestingModule({
    imports: [ MatSelectModule ],
    declarations: [ FooComponent ]
  })
  .compileComponents();
}));