[英]Angular 2 Unit Test: Custom Pipe error The pipe could not be found
I have a custom pipe called 'myPipe'.我有一个名为“myPipe”的自定义管道。 I am getting:
我正进入(状态:
The pipe 'myPipe' could not be found error
找不到管道“myPipe”错误
in my unit test ts.在我的单元测试中。 Pleas advice what to import and declare in my .spec.ts
请建议在我的 .spec.ts 中导入和声明什么
Here is my .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();
});
});
Thanks!谢谢!
You should be able to do this:你应该能够做到这一点:
import { MyPipe } from 'here put your custom pipe path';
TestBed.configureTestingModule({
declarations: [ MyComponentUnderTesting, MyPipe ]
})
I had the same problem, and fixed it by adding the following "mock pipe" to my spec.ts:我遇到了同样的问题,并通过在我的 spec.ts 中添加以下“模拟管道”来修复它:
import {Pipe, PipeTransform} from '@angular/core';
@Pipe({name: 'myPipe'})
class MockPipe implements PipeTransform {
transform(value: number): number {
// blah blah
return value;
}
}
Then you have to add MockPipe to the TestBed configureTestingModule declarations:然后你必须将 MockPipe 添加到 TestBed configureTestingModule 声明中:
TestBed.configureTestingModule({
declarations: [ MyComponentUnderTesting, MockPipe ]
})
I had almost the same pipe issue;我有几乎相同的管道问题; in cases of template parse errors, you need to take two steps:
在模板解析错误的情况下,您需要采取两个步骤:
Import the required pipe at the start like:在开始时导入所需的管道,如:
import {{ your_pipe_name }} from '../your/pipe/location'
; import {{ your_pipe_name }} from '../your/pipe/location'
;
Add it to your declaration:将其添加到您的声明中:
TestBed.configureTestingModule({ declarations: [ your_pipe ] });
Happy Coding!快乐编码!
It looks like you aliased/named your pipe but no one is answering based on this.看起来您对管道进行了别名/命名,但没有人基于此进行回答。 For example, if your pipe is named
myCustomPipe
but this is different than the class name for the pipe:例如,如果您的管道名为
myCustomPipe
但这与管道的类名不同:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'myCustomPipe',
pure: false
})
export class MyPipe implements PipeTransform {
// ....
}
then in your spec.ts
file you can import your pipe as follows or it will not be found:然后在您的
spec.ts
文件中,您可以按如下方式导入您的管道,否则将找不到它:
import { MyPipe as myCustomPipe } from 'path/to/pipe';
and in your beforeEach()
you need to reference the alias as both a declaration
and a provider
:并且在您的
beforeEach()
您需要将别名引用为declaration
和provider
:
beforeEach(() => {
TestBed.configureTestingModule({
imports: [ ... ],
declarations: [ myCustomPipe, etc],
providers: [ myCustomPipe, etc ]
}).compilecomponents();
// etc
});
you should start something like你应该开始类似的事情
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();
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.