简体   繁体   English

Angular 2 单元测试:自定义管道错误找不到管道

[英]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:在模板解析错误的情况下,您需要采取两个步骤:

  1. Import the required pipe at the start like:在开始时导入所需的管道,如:

    import {{ your_pipe_name }} from '../your/pipe/location' ; import {{ your_pipe_name }} from '../your/pipe/location'

  2. 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()您需要将别名引用为declarationprovider

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.

相关问题 Angular 4管道单元测试不适用于基本管道 - Angular 4 pipe unit test not working for basic pipe 角度/测试单元:失败:找不到 pipe“异步” - Angular/Testing unit: Failed: The pipe 'async' could not be found 如何为角度 4 的自定义排序管道编写单元测试用例? - How to write unit test cases for custom sort pipe in angular 4? 离子单元测试 - 找不到 pipe“日期” - ionic unit testing - the pipe 'date' could not be found 业力使用使用内置管道的自定义管道测试Angular X(5)组件 - Karma test an Angular X (5) Component with a custom pipe that uses a built in pipe 错误:NG0302:找不到管道“myFilter”! 运行 ng 测试时 - Error: NG0302: The pipe 'myFilter' could not be found! when running ng test 如何在角度单元测试中模拟组件中的 store.pipe - How to mock the store.pipe in a component in angular unit test 类型错误:无法读取未定义的属性“管道” - Angular 单元测试 - TypeError: Cannot read property 'pipe' of undefined - Angular Unit test 从服务订阅可观察时,角度单元测试管道不是功能 - Angular unit test pipe is not a function when subscribe observable from a service 自定义 Pipe 在运行单元测试时导致 ExpressionChangedAfterItHasBeenCheckedError - Custom Pipe causes the ExpressionChangedAfterItHasBeenCheckedError when running a unit test
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM