目前是否有办法使用_.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
答案 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);
});
});