2017-01-09 11 views
7

私は 'myPipe'と呼ばれるカスタムパイプを持っています。私は取得していますパイプ 'myPipe'は私のユニットテストでエラーが見つかりませんでした。私.spec.ts角2ユニットテスト:カスタムパイプエラーパイプが見つかりませんでした

にインポートして宣言する何

嘆願のアドバイスはここに私の.spec.ts

import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 
import { By } from '@angular/platform-browser'; 
import { DebugElement } from '@angular/core'; 

import { MyComponent } from './main-page-carousel.component'; 

describe('CarouselComponent',() => { 
    let component: MyComponent ; 
    let fixture: ComponentFixture<MyComponent>; 

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

    beforeEach(() => { 
    fixture = TestBed.createComponent(MyComponent); 
    component = fixture.componentInstance; 
    fixture.detectChanges(); 
    }); 

    it('should create',() => { 
    expect(component).toBeTruthy(); 
    }); 
}); 

感謝です!

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

@Pipe({name: 'myPipe'}) 
class MockPipe implements PipeTransform { 
    transform(value: number): number { 
     // blah blah 
     return value; 
    } 
} 

次に、あなたがする必要がある:

+0

あなたは 'myPipe'について話していますが、あなたのテストは' CarouselComponent'に関係していますか?代わりに 'myPipe'をインポートするべきではありませんか? –

+0

「パイプが見つかりません」という項目もチェックしてください:http://stackoverflow.com/questions/39007130/the-pipe-could-not-be-found-angular2-custom-pipe/40770507#40770507 – Karl

答えて

0

私は同じ問題を抱えていたし、私のspec.tsに次の「モックパイプ」を追加することによって、それを固定

import { TestBed, async } from '@angular/core/testing'; 
import { MyPipe } from 'here put your custom pipe path'; 

describe('Pipe: MyPipe',() => { 
    it('create an instance',() => { 
    let pipe = new MyPipe(); 
    expect(pipe).toBeTruthy(); 
    }); 
}); 
0

のようなものを開始する必要がありますTestBedのconfigureTestingModule宣言にMockPipeを追加してください:

TestBed.configureTestingModule({ 
    declarations: [ MyComponentUnderTesting, MockPipe ] 
}) 
10

これを行うことができます:

import { MyPipe } from 'here put your custom pipe path'; 
    TestBed.configureTestingModule({ 
    declarations: [ MyComponentUnderTesting, MyPipe ] 
    }) 
関連する問題