【问题标题】:Angular 4 pipe unit test not working for basic pipeAngular 4 管道单元测试不适用于基本管道
【发布时间】:2018-02-14 01:31:06
【问题描述】:

Angular 4 管道单元测试不适用于基本管道

我得到的业力错误:TypeError: Cannot read property '0' of undefined

这是我的管道(非常基础):

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

@Pipe({
  name: "userPipe",
  pure: true
})

export class MyFilter implements PipeTransform {
   transform(items: any, [listOfItems]: any) {

     // Example of a pipe: listOfItems = listOfItems.filter(item => item.type === "test");

     return listOfItems;
  }
}

这是我的管道单元测试:

import { MyFilter } from "./user.pipe";

describe("userPipe", () => {
  let pipe: MyFilter;
  let fakeResponse = ["abc"];

  beforeEach(() => {
    pipe = new MyFilter();
  });

  it("transforms abc to abc", () => {
    expect(pipe.transform(fakeResponse)).toEqual(fakeResponse);
  });
});

【问题讨论】:

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


    【解决方案1】:

    首先更新你的转换函数,测试将使用这段代码

    export class MyFilter implements PipeTransform {
       transform( items: Array<any> ) {
         return listOfItems;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-23
      • 2018-11-03
      • 2020-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多