【发布时间】:2015-10-27 23:28:26
【问题描述】:
我在我的应用程序中使用 moment.js 作为日期/时间,但它似乎不能很好地与 Jasmine 的模拟功能配合使用。我在下面整理了一个测试套件来显示我的问题:
jasmine.clock().mockDate 似乎暂时无法正常工作,而 Date 可以正常工作。
describe('Jasmine tests', function () {
beforeEach(function() {
jasmine.clock().install();
});
afterEach(function() {
jasmine.clock().uninstall();
});
// Pass
it('uses the mocked time with Date', function() {
var today = new Date('2015-10-19');
jasmine.clock().mockDate(today);
expect(new Date().valueOf()).toEqual(today.valueOf());
});
// Fail
it('uses the mocked time with moment', function() {
var today = moment('2015-10-19');
jasmine.clock().mockDate(today);
expect(moment().valueOf()).toEqual(today.valueOf());
});
});
为什么Date 能按预期工作,而moment 不能? moment 不是在后台使用 Date 吗?
使用 Jasmine 模拟 moment 的正确方法是什么?
【问题讨论】:
标签: javascript jasmine momentjs