久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

用 moment.js 模擬茉莉花日期

Jasmine date mocking with moment.js(用 moment.js 模擬茉莉花日期)
本文介紹了用 moment.js 模擬茉莉花日期的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我在我的應用程序中使用 moment.js 作為日期/時間,但它似乎不能很好地與 Jasmine 的模擬功能配合使用.我在下面整理了一個測試套件來顯示我的問題:

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:

jasmine.clock().mockDate 似乎暫時不起作用,而對于 Date 卻可以正常工作.

jasmine.clock().mockDate doesn't seem to work for moment, while it works fine for 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 嗎?

Why does Date work as expected while moment does not? Isn't moment using Date under the hood?

使用 Jasmine 模擬 moment 的正確方法是什么?

What is the right way to mock moment using Jasmine?

推薦答案

jasmine.clock().mockDate 期望 Date 作為輸入.Datemoment 不完全兼容.如果您在規范本身中提供要模擬的日期,您可以簡單地使用 Date 代替.

jasmine.clock().mockDate expects Date as input. Date and moment are not fully compatible. If you provide the to-be-mocked date in the spec itself you could simply use Date there instead.

如果您的代碼生成要模擬的時刻,或者您更愿意使用時刻 API,請查看 moment.toDate().此方法返回 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 Date object backing a moment.

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

這篇關于用 moment.js 模擬茉莉花日期的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I get my jasmine tests fixtures to load before the javascript considers the document to be quot;readyquot;?(在 javascript 認為文檔“準備好之前,如何讓我的 jasmine 測試裝置加載?) - IT屋-程序員軟件開發技術
What do jasmine runs and waitsFor actually do?(jasmine 運行和等待實際上是做什么的?)
How to provide mock files to change event of lt;input type=#39;file#39;gt; for unit testing(如何提供模擬文件來更改 lt;input type=filegt; 的事件用于單元測試)
How to unit test a chained method using Jasmine(如何使用 Jasmine 對鏈式方法進行單元測試)
How do I inject $rootScope into an AngularJS unit test?(如何將 $rootScope 注入 AngularJS 單元測試?)
Jasmine - How to spy on a function call within a function?(Jasmine - 如何監視函數中的函數調用?)
主站蜘蛛池模板: 色男人的天堂 | 中文二区| 久久精品16 | 久久这里只有精品首页 | 精品成人av| 亚洲成av| 欧美视频区 | 国产精品视频一区二区三区四蜜臂 | 国产精品久久久久一区二区三区 | 免费精品久久久久久中文字幕 | 国产精品一区二区三区四区五区 | 日韩一级一区 | 亚洲在线一区二区 | 久久99精品久久久久久噜噜 | 亚洲精品日韩欧美 | 国产亚洲精品精品国产亚洲综合 | 国产亚洲一区二区三区在线观看 | 在线一区 | 亚洲精品日韩欧美 | 黄色在线观看网站 | 天堂视频一区 | 国产中文字幕在线 | 中文字幕av在线播放 | 中文av在线播放 | 久久久久久久久蜜桃 | 国产精品久久久久久久久久久久久 | 亚洲人人舔人人 | 国产精品欧美一区二区三区 | 国产精品久久久久久一区二区三区 | 午夜精品久久久久久久久久久久 | 亚洲一区 中文字幕 | 自拍偷拍亚洲一区 | 狠狠久久久 | 一级片视频免费 | 日一区二区三区 | 久久福利电影 | 一级毛片在线播放 | 欧美欧美欧美 | 日本精品久久久久 | 亚洲欧美一区二区三区视频 | 国产中文字幕在线观看 |