As per the example given in official 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);
:
});
});
It reports jasmine.clock is not a function
. I'm using following dependencies in my project
"devDependencies": {
"jasmine": "^2.5.2",
"jasmine-node": "^1.14.5"
}
I also tried to mock Date object. But since I'm in node not on browser, I don't find/get window
to mock any method.
First you need to install jasmine-mockdate .
Then test as follow:
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);
});
});
Hope this help.
I found that I was using unofficial jasmine library. So I should have used "jasmine-core" instead of "jasmine-node" to get support of latest jasmine release. However it still was failing to mock dates.
So I separated a function to return date. then;
spyOn
to mock the behaviour of date function. rewire
which helps to access private variable or methods of a nodejs module. So it can also be used to spyOn
date function instead of exporting it just because of mocking.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.