如何在Angular 4上使用Typescript 2和Jasmine 2模拟或间谍没有对象的外部函数

时间:2017-06-08 01:11:13

标签: angular unit-testing typescript jasmine mocking

我想检查一个函数haveBeenCalledWith()我需要的参数,这样我就可以检查我的调用是否正确,而且没有必要调用真正的方法。

我尝试了很多解决方案。我发现的一些(Using Jasmine to spy on a function without an object)和(How to spy a function without root object

我有一个可能对http://plnkr.co/edit/xmLCNffTvyD8rp9VcRex

有帮助的尝试

我目前使用的外部库是date-fns

我在Angular 4中创建了一个Pipe,就像这样:

import { Pipe, PipeTransform } from '@angular/core';
// It works if I import all 'date-fns', but it gives me issues with three shaking, so that's not a proper solution
import * as format from 'date-fns/format'; 

@Pipe({
    name: 'dateFnsFormat'
})
export class DateFnsFormatPipe implements PipeTransform {

    transform(value: any, args?: any): any {
        return format(value, args);
    }

}

所以我想测试format函数的调用。

如果我在没有嘲笑的情况下进行测试它有效:

import * as format from 'date-fns/format';

const xmasDay = '2017-12-25 12:30:00';

// Works with real function
it('should transform the date from Y-m-d H:m:s to d/m/Y', () => {
    const dateFnsFormatPipe = new DateFnsFormatPipe();
    expect(dateFnsFormatPipe.transform(xmasDay, 'DD/MM/YYYY')).toBe('25/12/2017');
});


// Works with real function
it('should transform the date from Y-m-d H:m:s to d/m/Y H:m:s', () => {
    const dateFnsFormatPipe = new DateFnsFormatPipe();
    expect(dateFnsFormatPipe.transform(xmasDay, 'DD/MM/YYYY HH:mm:ss')).toBe('25/12/2017 12:30:00');
});

贝娄,我展示了我的所有尝试和以下错误:

// #1 Attempt with spyOn(window, 'function');
// @link https://stackoverflow.com/a/9511646/3415716
// Error: Error: format() method does not exist
it('#1 should transform the date from Y-m-d H:m:s to d/m/Y H:m:s', () => {
    spyOn(window, 'format');

    const dateFnsFormatPipe = new DateFnsFormatPipe();
    dateFnsFormatPipe.transform(xmasDay, 'DD/MM/YYYY HH:mm:ss');
    expect(window.format).toHaveBeenCalled();
});

// #1b Attempt with spyOn(global, 'function');
// @link https://stackoverflow.com/a/9511646/3415716
// Error: Error: format() method does not exist
it('#1b should transform the date from Y-m-d H:m:s to d/m/Y H:m:s', () => {
    spyOn(global, 'format');

    const dateFnsFormatPipe = new DateFnsFormatPipe();
    dateFnsFormatPipe.transform(xmasDay, 'DD/MM/YYYY HH:mm:ss');
    expect(global.format).toHaveBeenCalled();
});

// #2 Attempt with jasmine.createSpy().and.callFake(function);
// @link https://stackoverflow.com/a/29922957/3415716
// Error: Expected spy unknown to have been called.
it('#2 should transform the date from Y-m-d H:m:s to d/m/Y H:m:s', () => {
    const test = jasmine.createSpy().and.callFake(format);

    const dateFnsFormatPipe = new DateFnsFormatPipe();
    dateFnsFormatPipe.transform(xmasDay, 'DD/MM/YYYY HH:mm:ss');
    expect(test).toHaveBeenCalled();
});


// #3 Attempt with jasmine.createSpy().and.callFake(function);
// @link https://stackoverflow.com/a/29922957/3415716
// Error: Expected spy testSpy to have been called.
it('#3 should transform the date from Y-m-d H:m:s to d/m/Y H:m:s', () => {
    const mySpy = jasmine.createSpy('testSpy', format).and.callThrough();

    const dateFnsFormatPipe = new DateFnsFormatPipe();
    dateFnsFormatPipe.transform(xmasDay, 'DD/MM/YYYY HH:mm:ss');
    expect(mySpy).toHaveBeenCalled();
});

// #4 Attempt with jasmine.createSpy(global, 'function');
// @link https://groups.google.com/forum/#!searchin/jasmine-js/spy$20function%7Csort:relevance/jasmine-js/a3gNCMMd3UM/6iP8jpfIAQAJ
//  Expected spy global to have been called.
it('#4 should transform the date from Y-m-d H:m:s to d/m/Y H:m:s', () => {
    const mySpy = jasmine.createSpy('global', format);
    const mySpyFormat = jasmine.createSpy('format');

    const dateFnsFormatPipe = new DateFnsFormatPipe();
    dateFnsFormatPipe.transform(xmasDay, 'DD/MM/YYYY HH:mm:ss');
    expect(mySpy).toHaveBeenCalled();
    expect(mySpyFormat).toHaveBeenCalled();
});

PS:我使用的示例是date-fns,但是,我想用不同的库实现相同的想法。此外,由于 webpack (阅读https://github.com/ReactiveX/rxjs/issues/1888)的三次摇晃,我不想要包含主文件。

1 个答案:

答案 0 :(得分:0)

如果您尝试类似的操作...

it('should', () => {
      const dffp = new DateFnsFormatPipe();
      spyOn(DateFnsFormatPipe, 'transform').and.callThrough();
      spyOn(dffp, 'format');

      dateFnsFormatPipe.transform(xmasDay, 'DD/MM/YYYY');

      expect(dffp.format).toHaveBeenCalledWith(xmasDay, 'DD/MM/YYYY');
    })

我一直在使用这种方法,效果很好。您可能需要进行一些调整才能为您工作,但是应该关闭