用茉莉花钟测试油门

时间:2017-12-05 21:19:19

标签: javascript jasmine lodash throttling

目前是否有办法使用_.throttle测试lodash jasmine.clock?我在2015年找到了人们说不可能的答案,这里是我发现的一个例子:https://github.com/jasmine/jasmine/issues/361

我的代码看起来像这样:

let methodTested =  _.throttle(() => callMethod, {trailing: true, leading: false }); // this method is imported

beforeEach(function() {
  methodTested();
  jasmine.clock().tick(100);
});

it('callMethod to be called', function() {
  expect(callMethod).toHaveBeenCalled();
});

我正在使用jasmine 2.2和lodash 3.10

1 个答案:

答案 0 :(得分:0)

这对我有用

Lodash:



'use strict';

const _ = require('lodash');

describe('foo:', () => {
  let now;

  beforeEach(() => {
    jasmine.clock().install();
    jasmine.clock().mockDate();
 
    // const nowTime = Date.UTC(2017, 0, 1); // 1483228800 * 1000, now
    // jasmine.clock().mockDate(new Date(nowTime));
  });

  afterEach(() => {
    jasmine.clock().uninstall();
  });

  it('bar', () => {
    const callMethod = jasmine.createSpy();
    const throttled = _.throttle(() => callMethod(), 100);

    const t = setInterval(() => {
       throttled()
    }, 50);

    jasmine.clock().tick(400);

    expect(callMethod).toHaveBeenCalledTimes(4);

    clearInterval(t);
  });

});