jac*_*wah 20 javascript jasmine momentjs
我在我的应用程序中使用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());
});
});
Run Code Online (Sandbox Code Playgroud)
为什么没有Date按预期工作moment?是不是在引擎盖下moment使用Date?
moment使用Jasmine 模拟的正确方法是什么?
jac*_*wah 35
jasmine.clock().mockDate期待Date作为输入.Date并且moment不完全兼容.如果你在规范中提供了被模拟的日期,你可以简单地Date在那里使用.
如果您的代码生成了您想要模拟的时刻,或者您更愿意使用时刻API,请查看moment.toDate().此方法返回Date支持片刻的对象.
it('uses the mocked time with moment', function() {
var today = moment('2015-10-19').toDate();
jasmine.clock().mockDate(today);
expect(moment().valueOf()).toEqual(today.valueOf());
});
Run Code Online (Sandbox Code Playgroud)