2017-01-09 24 views
7

Tôi có một đường ống tùy chỉnh được gọi là 'myPipe'. Tôi nhận được ống 'myPipe' không thể tìm thấy lỗi trong ts thử nghiệm đơn vị của tôi.Góc 2 Đơn vị kiểm tra: Lỗi ống tùy chỉnh Đường ống không thể được tìm thấy

Pleas lời khuyên gì để nhập khẩu và tuyên bố trong .spec.ts tôi

đây là .spec.ts tôi

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

Cảm ơn!

+0

bạn đang nói về 'myPipe' nhưng thử nghiệm của bạn có liên quan đến 'CarouselComponent'? bạn không nên nhập 'myPipe' thay thế? –

+0

Kiểm tra mục nhập "không tìm thấy đường ống" này: http://stackoverflow.com/questions/39007130/the-pipe-could-not-be-found-angular2-custom-pipe/40770507#40770507 – Karl

Trả lời

0

bạn nên bắt đầu một cái gì đó giống như

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

tôi đã cùng một vấn đề, và cố định nó bằng cách thêm những điều sau đây "ống giả" để spec.ts tôi:

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

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

Sau đó, bạn phải thêm MockPipe vào các khai báo TestBed configureTestingModule:

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

Bạn sẽ có thể thực hiện việc này:

import { MyPipe } from 'here put your custom pipe path'; 
    TestBed.configureTestingModule({ 
    declarations: [ MyComponentUnderTesting, MyPipe ] 
    }) 
Các vấn đề liên quan