gpt4 book ai didi

javascript - jasmine mockDate 总是休息一个月

转载 作者:行者123 更新时间:2023-11-30 15:24:55 26 4
gpt4 key购买 nike

我正在尝试使用 jasmine 的 mockDate() 编写时间相关的单元测试:

it("cookie_expiration() is 30 days out", function () {
var baseTime = new Date(2016, 10, 1, 0, 0, 0, 0);
jasmine.clock().mockDate(baseTime);
console.info( new Date() );
...
});

我认为这正是他们在 the documentation 中展示的内容但我在控制台中看到的是:Tue Nov 01 2016 00:00:00 GMT-0700 (PDT)

"devDependencies": {
"jasmine": "^2.5.3",
"karma": "^1.5.0",
"karma-chrome-launcher": "^2.0.0",
"karma-jasmine": "^1.1.0",
"karma-junit-reporter": "^1.2.0",
"karma-phantomjs-launcher": "^1.0.4"
}

最佳答案

根据 Date | MDN ,

note that the month is 0-based

因此,这不是 jasminemockDate 的问题。这是 native Date

这是显示现在是 11 月的片段:)

var baseTime = new Date(2016, 10, 1, 0, 0, 0, 0);
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

console.log("BaseTime month is " + monthNames[baseTime.getMonth()]);

关于javascript - jasmine mockDate 总是休息一个月,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43150846/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com