在 Jasmine 中测试具有构造函数方法的 Angular2 Pipe [英] Test an Angular2 Pipe that has a constructor method in Jasmine

查看:12
本文介绍了在 Jasmine 中测试具有构造函数方法的 Angular2 Pipe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在测试具有构造函数的 Angular Pipe 时遇到了第一个障碍.

I am falling at the first hurdle testing an Angular Pipe that has a constructor.

我的管道如下:

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

@Pipe({
  name: 'reverse',
  pure: false
})
export class ReversePipe implements PipeTransform {

  private cached: Array<any>;
  private differ: IterableDiffer<Array<any>>;

  constructor(private differs: IterableDiffers) {
    this.differ = this.differs.find([]).create(null);
  }

  transform(array: Array<any>): Array<any> {
    // TODO: Throw an error if `array` isn't an Array
    if (Array.isArray(array) === false) return [];

    const changes = this.differ.diff(array);

    if (changes) this.cached = array.slice().reverse();

    return this.cached;
  }
}

我通过几个教程相信使用 IterableDiffer 来提高效率是正确的.但这不是这个问题的主题.

I believe through several tutorials that it is correct to use an IterableDiffer for efficiency. But that isn't the topic of this question.

我认为需要构造函数的事实是这个简单测试失败的根源:

The fact that a constructor is required, I believe is the root of this simple test failing:

import { ReversePipe } from './reverse.pipe';

describe('ReversePipe', () => {
  it('create an instance', () => {
    const pipe = new ReversePipe();
    expect(pipe).toBeTruthy();
  });
});

测试失败并出现错误:TypeError: Cannot read property 'find' of undefined

我(可能不正确)假设是因为 differs 需要在测试中注入,因为错误消息表明它是 undefined.

This I (probably incorrectly) assume is because differs needs injecting in the test as the error message suggests that it is undefined.

我的思路是否正确?我应该如何为 Pipe 编写一个简单的测试?

Am I along the right lines and how should I write a simple test for the Pipe?

我尝试将 IterableDiffers 注入到正在测试的管道中;虽然这已经纠正了以前的错误,但我没有遇到新错误 Error: Can't resolve all parameters for IterableDiffers: (?).

I have tried to inject IterableDiffers into the Pipe being tested; while that has rectified the previous error I am not faced with a new one Error: Can't resolve all parameters for IterableDiffers: (?).

在终端中,错误 无法调用类型缺少调用签名的表达式.类型IterableDiffers"没有兼容的调用签名. 已显示.

In the terminal, the error Cannot invoke an expression whose type lacks a call signature. Type 'IterableDiffers' has no compatible call signatures. is shown.

两者都在用不同的语言描述同一个问题.

Both are describing the same problem just in different language.

我更新的测试是:

import { IterableDiffer, IterableDiffers } from '@angular/core';
import { TestBed, inject } from '@angular/core/testing';

import { ReversePipe } from './reverse.pipe';

describe('ReversePipe', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [IterableDiffers]
    });
  });

  // it('create an instance', () => {
  //   const array = [ 1, 2, 3 ];
  //   const pipe = new ReversePipe(new IterableDiffers);
  //   expect(pipe).toBeTruthy();
  // });

  it('create an instance', inject([IterableDiffers], (iterableDiffers: IterableDiffers) => {
    const array = [ 1, 2, 3 ];
    const pipe = new ReversePipe(iterableDiffers);

    expect(pipe).toBeTruthy();
  }));
});

非常感谢任何和所有帮助.

Any and all help is very much appreciated.

推荐答案

我的更新测试快到了.您不需要提供 IterableDiffers:

I was very nearly there with my updated test. You do not need to provide IterableDiffers:

import { IterableDiffers } from '@angular/core';
import { TestBed, inject } from '@angular/core/testing';

import { ReversePipe } from './reverse.pipe';

describe('ReversePipe', () => {
  it('should create an instance', inject([ IterableDiffers ], (iterableDiffers: IterableDiffers) => {
    const pipe = new ReversePipe(iterableDiffers);

    expect(pipe).toBeTruthy();
  }));

  it('should reverse the array of type Array<number>', inject([ IterableDiffers ], (iterableDiffers: IterableDiffers) => {
    const array = [ 1, 2, 3 ];
    const pipe = new ReversePipe(iterableDiffers);

    expect(pipe.transform(array)).toEqual([ 3, 2, 1 ]);
  }));

  it('should reverse the array of type Array<string>', inject([ IterableDiffers ], (iterableDiffers: IterableDiffers) => {
    const array = [ 'apple', 'banana', 'clementine' ];
    const pipe = new ReversePipe(iterableDiffers);

    expect(pipe.transform(array)).toEqual([ 'clementine', 'banana', 'apple' ]);
  }));
});

我还注意到我在 reverse.pipe.spec.ts 中有一个不必要的 if 语句:

I also noticed I had an unnecessary if statement in reverse.pipe.spec.ts:

// TODO: Throw an error if `array` isn't an Array
if (Array.isArray(array) === false) return [];

transform 的第一个参数总是一个Array;当然,如果参数不是 Array,TypeScript 编译器会抛出 TypeError.

The first argument of transform would always be an Array; of course, the TypeScript compiler would throw a TypeError if the argument was anything other than an Array.

为了完整起见,我的管道是:

For completeness my Pipe is:

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

@Pipe({
  name: 'reverse',
  pure: false
})
export class ReversePipe implements PipeTransform {

  private cached: Array<any>;
  private differ: IterableDiffer<Array<any>>;

  constructor(private differs: IterableDiffers) {
    this.differ = this.differs.find([]).create(null);
  }

  public transform(array: Array<any>): Array<any> {
    const changes = this.differ.diff(array);

    if (changes) this.cached = array.slice().reverse();

    return this.cached;
  }
}

这篇关于在 Jasmine 中测试具有构造函数方法的 Angular2 Pipe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆