2017-01-09 55 views
7

我有一個名爲'myPipe'的自定義管道。我得到我的單元測試ts中找不到'myPipe'管道錯誤。這裏Angular 2單元測試:自定義管道錯誤找不到管道

普萊斯建議如何導入並宣佈在我.spec.ts

是我.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(); 
    }); 
}); 

謝謝!

+0

你說的是'myPipe',但是你的測試和'CarouselComponent'有關?你不應該導入'myPipe'? –

+0

檢查此「管道未找到」條目:http://stackoverflow.com/questions/39007130/the-pipe-could-not-be-found-angular2-custom-pipe/40770507#40770507 – Karl

回答

0

你應該開始像

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

我有同樣的問題,並通過添加下面的「模擬管道」我spec.ts固定它:

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

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

然後,你必須將MockPipe添加到TestBed configureTestingModule聲明中:

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

您應該可以執行此操作:

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