目前,我正在重载提供程序以使用像这样的模拟服务:

beforeEach(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
    tcb.overrideProviders(AddFieldToObjectDropdownComponent,
        [
             provide(ServiceA, { useClass: MockServiceA })),
             provide(ServiceB, { useClass: MockServiceB }))
        ])
    ...

我想对组件使用的管道做同样的事情。我尝试了provide(PipeA, { useClass: MockPipeA })provide(PipeA, { useValue: new MockPipeA() }),但是都没有用。

最佳答案

您可以在declarationsTestBed中添加模拟管道:

TestBed.configureTestingModule({
             declarations: [
                 AppComponent,
                 MockPipe
             ],
            ...
MockPipe需要具有原始名称的@Pipe装饰器。

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

@Pipe({name: 'pipename'})
class MockPipe implements PipeTransform {
    transform(value: number): number {
        //Do stuff here, if you want
        return value;
    }
}

关于unit-testing - 测试组件时如何模拟Pipe,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39293258/

10-11 11:37