2016-11-11 56 views
-1

我有一個角2組分,其做工精細,但是當我運行這段代碼ng test通過我得到的followind錯誤信息所有測試:角2測試得到錯誤

Can't bind to 'ngModel' since it isn't a known property of 'mdl-textfield'.                   
    1. If 'mdl-textfield' is an Angular component and it has 'ngModel' input, then verify that it is part of this module.         
    2. If 'mdl-textfield' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schema' of this component to suppress this message.  
    ("ditedIndex !== i">{{role.name}}</span>                            
         <mdl-textfield *ngIf="editedIndex === i" [ERROR ->][(ngModel)]="roles[i].name" class="full-width" value={{role.name}}></mdl-textfi 

場>

這裏是我的HTML:

<tr *ngFor="let role of roles; let i = index;" (click)="doSelect(role, i)"> 
      <td class="table-id">{{ i + 1 }}</td> 
      <td> 
       <span *ngIf="editedIndex !== i">{{role.name}}</span> 
       <mdl-textfield *ngIf="editedIndex === i" [(ngModel)]="role.name" class="full-width" value={{role.name}}></mdl-textfield> 
      </td> 
      <td> 
       <span *ngIf="editedIndex !== i">{{role.description}}</span> 
       <mdl-textfield *ngIf="editedIndex === i" [(ngModel)]="role.description" class="full-width" value={{role.description}}></mdl-textfield> 
      </td> 
      <td> 
       <button *ngIf="editedIndex === i" mdl-button mdl-button-type="mini-fab" mdl-colored="primary" (click)="doSave($event)" 
        mdl-ripple><mdl-icon>edit</mdl-icon></button> 
       <button *ngIf="editedIndex === i" mdl-button mdl-button-type="mini-fab" mdl-colored="accent" (click)="doDelete($event, i)" 
        mdl-ripple><mdl-icon>remove</mdl-icon></button> 
      </td> 
     </tr> 

角色是一個有2個屬性的模型。

爲什麼它運行正常,但它在測試中失敗!

回答

0

因爲我使用的自定義組件,我發現我不得不進口CUSTOM_ELEMENTS_SCHEMA到app.component.spec.ts

import { CUSTOM_ELEMENTS_SCHEMA} from '@angular/core'; 

,並用它在架構

TestBed.configureTestingModule({ 
    declarations: [ 
    AppComponent, 
    ], 
    imports: [ 
    MdlModule, 
    ], 
    schemas: [CUSTOM_ELEMENTS_SCHEMA] 
}); 
1

您需要配置任何所需的模塊/組件/等測試牀。你唯一免費獲得的是CommonModule。其他的一切,你需要從頭開始配置。這意味着增加了FormsModule,不管模塊(組件/指令/等)用於MDL

TestBed.configureTestingModule({ 
    imports: [ 
    FormsModule, 
    AnyMDLModule 
    ], 
    declarations: [], 
    providers: [] 
})