【问题标题】:Сonfigure testBed for Pipe为管道配置 testBed
【发布时间】:2021-06-23 20:14:01
【问题描述】:

同事们,谁能告诉我,我正在尝试为管道配置 testBed,它只有在我将管道本身放入提供程序时才有效,我将对其进行测试。 我无法以任何方式解决这个问题,你能告诉我这是怎么回事吗? 第二个问题:除了transform方法,我在pipe里还有一个私有方法,怎么测试??

【问题讨论】:

    标签: angular unit-testing jasmine pipe karma-jasmine


    【解决方案1】:

    我会尝试这样做this 方式。

    但是拥有它作为提供者很好,而且你正在抓住它。

    要测试私有方法,您可以使环境正确,以便遍历私有方法。

    @Pipe({ name: 'xyz', pure: true })
    export class SdDateFormatPipe implements PipeTransform {
       value: number;
       constructor(private datePipe: DatePipe, private configService: ConfigService) {}
    
       transform(value: string): string {
         if (value.includes('a')) {
           this.setValue(1);
         } else {
           this.setValue(2);
         }
       }
    
       private setValue(num: number) {
         this.value = num;
       }
    }
    
    it("should set value to 1 if the string has a", () => {
      pipe.transform('abc');
      expect(pipe.value).toBe(1);
    });
    
    it("should set value to 2 if the string does not have an a", () => {
      pipe.transform('xyz');
      expect(pipe.value).toBe(2);
    });
    

    所以在一天结束的时候,看看你从私有方法中得到了什么,并断言它做了它应该做的事情。

    【讨论】:

      【解决方案2】:

      如果没有依赖注入,您可以使用 let pipe = new SdDateFormatPipe() 进行实例化。

      关于私有方法,它可能在公共方法中被调用,所以为了在你的测试中覆盖它,正确和推荐的方法是制作一个调用私有方法的测试用例。

      【讨论】:

        猜你喜欢
        • 2020-01-02
        • 2021-07-25
        • 1970-01-01
        • 2023-03-28
        • 2019-04-17
        • 1970-01-01
        • 1970-01-01
        • 2018-12-09
        • 1970-01-01
        相关资源
        最近更新 更多