根据官方documentation中提供的示例,
describe("Mocking the Date object", function(){
it("mocks the Date object and sets it to a given time", function() {
var baseTime = new Date(2013, 9, 23);
jasmine.clock().mockDate(baseTime);
:
});
});
报告jasmine.clock is not a function
。我在项目中使用了以下依赖项
"devDependencies": {
"jasmine": "^2.5.2",
"jasmine-node": "^1.14.5"
}
我也试过模拟Date对象。但由于我在节点上而不在浏览器上,因此我找不到/ window
来模拟任何方法。
答案 0 :(得分:0)
首先,您需要安装jasmine-mockdate。
然后测试如下:
describe("Mocking the Date object", function () {
it("mocks the Date object and sets it to a given time", function () {
var baseTime = new Date(2013, 9, 23);
jasmine.clock().install();
jasmine.clock().mockDate(baseTime);
jasmine.clock().tick(50);
expect(new Date().getTime()).toEqual(baseTime.getTime() + 50);
});
});
希望得到这个帮助。
答案 1 :(得分:0)
我发现我使用的是非官方的茉莉花库。所以我应该使用“jasmine-core”而不是“jasmine-node”来获得最新茉莉花发布的支持。然而它仍未能模拟日期。
所以我把一个函数分开来返回日期。然后;
spyOn
来模拟日期函数的行为。rewire
,它有助于访问nodejs模块的私有变量或方法。所以它也可以用于spyOn
日期函数,而不是仅仅因为模拟而导出它。