茉莉花模拟日期总是一个月

时间:2017-03-31 23:04:42

标签: javascript jasmine karma-jasmine jasmine2.0

我试图使用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"
}

1 个答案:

答案 0 :(得分:2)

根据Date | MDN

  

请注意,月份是从0开始的

因此,jasmine mockDate不是问题。这是原生的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()]);