2017-11-09 21 views
0

テーブルの検索バーにパイプを使用しています。私はapp.module.tsの宣言でフィルタを追加したカスタムフィルタが原因で角度テストに失敗する

 The pipe 'filter' could not be found (" 
     <tbody> 
     <tr *ngFor="l[ERROR ->]et i of tutors | filter : searchText" name="tutors_list"> 
      <td>{{i.name}}</td> 

:私は次のエラーを取得しています。ここで

は私のカスタムフィルタである:あなたのアプリケーションモジュールで

import { Pipe, PipeTransform } from '@angular/core'; 

@Pipe({ 
    name: 'filter' 
}) 
export class FilterPipe implements PipeTransform { 

    transform(items: any[], searchText: string) { 
    if (searchText === undefined) return items; 

    return items.filter(function(i){ 
    if(i.name.toLowerCase().includes(searchText.toLowerCase())) 
    { return i;} 
    else if(i.department.toLowerCase().includes(searchText.toLowerCase())) 
    { return i;} 
    else if(i.email.toLowerCase().includes(searchText.toLowerCase())) 
    { return i;} 
    }); 

    } 

} 

答えて

0

declarations配列であなたのFilterPipeクラスが含まれています。

// App.module.ts 
@NgModule({ 
    declarations: [ 
    ..., 
    FilterPipe // This i what you are missing. 
    ], 
... 
export class AppModule { 
    ... 
} 

ユニットテストでは、これはTestBed内で宣言する必要があります。何かのように、

... 

    TestBed.configureTestingModule({ 
     declarations: [ FilterPipe ], // Declare your filter here 
       ... 
    }); 

... 
関連する問題