javascript 用 moment.js 模拟茉莉花日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33380311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 16:26:12  来源:igfitidea点击:

Jasmine date mocking with moment.js

javascriptjasminemomentjs

提问by jacwah

I'm using moment.js for date/time in my application, but it seems like it doesn't play well with Jasmine's mocking capabilities. I've put together a test suite below that shows my issue:

我在我的应用程序中使用 moment.js 作为日期/时间,但它似乎不能很好地与 Jasmine 的模拟功能配合使用。我在下面整理了一个测试套件来显示我的问题:

jasmine.clock().mockDatedoesn't seem to work for moment, while it works fine for Date.

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());
    });
});

Why does Datework as expected while momentdoes not? Isn't momentusing Dateunder the hood?

为什么Date按预期工作而moment没有?不是在引擎盖下moment使用Date吗?

What is the right way to mock momentusing Jasmine?

moment使用 Jasmine进行模拟的正确方法是什么?

回答by jacwah

jasmine.clock().mockDateexpects Dateas input. Dateand momentare not fully compatible. If you provide the to-be-mocked date in the spec itself you could simply use Datethere instead.

jasmine.clock().mockDate期望Date作为输入。Date并且moment不完全兼容。如果您在规范本身中提供了待模拟的日期,则可以简单地使用该日期Date

If your code generates a moment you want to mock, or you'd rather use the moment API, take a look at moment.toDate(). This method returns the Dateobject backing a moment.

如果您的代码生成了您想要模拟的时刻,或者您更愿意使用时刻 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());
});

回答by infomaniac

回答by Market

I was trying to find an alternative to jasmineor even other mock frameworks to avoid dependencies.

我试图找到替代jasmine甚至其他模拟框架来避免依赖关系

const currentToday = moment().toDate();
console.log(`currentToday:`, currentToday)

const newToday = moment('1980-01-01').toDate();
console.log(`newToday    :`, newToday);

Date.now = () => {
  return newToday
};

const fakedToday = moment().toDate();
console.log(`fakedToday  :`, fakedToday)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

currentToday: 2019-09-17T15:26:12.763Z
newToday    : 1980-01-01T00:00:00.000Z
fakedToday  : 1980-01-01T00:00:00.001Z