首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >角2单元测试:自定义管道错误找不到管道

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

Stack Overflow用户
提问于 2017-01-09 07:58:19
回答 5查看 45.9K关注 0票数 76

我有一个定制的管道,叫做“我的管道”。我得到了:

找不到管道'myPipe‘错误

在我的单元测试中。请建议在我的.spec.ts中导入和声明什么

这是我的.spec.ts

代码语言:javascript
运行
复制
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();
  });
});

谢谢!

EN

回答 5

Stack Overflow用户

发布于 2017-08-03 17:13:42

你应该能够做到这一点:

代码语言:javascript
运行
复制
import { MyPipe } from 'here put your custom pipe path';

TestBed.configureTestingModule({
    declarations: [ MyComponentUnderTesting, MyPipe ]
})
票数 141
EN

Stack Overflow用户

发布于 2017-06-12 21:30:52

我也遇到了同样的问题,并通过在我的spec.ts中添加以下“模拟管道”来修复它:

代码语言:javascript
运行
复制
import {Pipe, PipeTransform} from '@angular/core';

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

然后,必须将MockPipe添加到TestBed configureTestingModule声明中:

代码语言:javascript
运行
复制
TestBed.configureTestingModule({
  declarations: [ MyComponentUnderTesting, MockPipe ]
})
票数 10
EN

Stack Overflow用户

发布于 2020-03-24 16:59:03

我遇到了几乎相同的管道问题;在模板解析错误的情况下,您需要执行两个步骤:

  1. 在开始时导入所需的管道如下: import {{ your_pipe_name }} from '../your/pipe/location'
  2. 将其添加到您的声明中: TestBed.configureTestingModule({ declarations: [ your_pipe ] });

编码愉快!

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41543374

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档