admin 管理员组文章数量: 1086019
I am writing a Jasmine unit test for a function in which dates are pared. I want to provide a fake date to be used for today's date. Therefore I am spying on the Date
method on the window object and returning a predefined date.
This works fine, but in the function I'm testing, I am also reading dates from a string and calling new Date(yyyy, mm, dd)
to turn them into dates. When this occurs, these values are replaced with the mock date I provided.
Here's an example:
var checkDate = function () {
return { today: new Date(), anotherDay: new Date(2016, 0, 1) }
};
var createDate = function (year, month, date) {
var overrideDate = new Date(year, month, date);
spyOn(window, 'Date').andCallFake(function () {
return overrideDate;
})
}
var dates;
describe("checkDate", function() {
beforeEach(function() {
createDate(2015, 11, 1);
dates = checkDate();
})
it("today has a value of 12/1/2015", function() {
expect(dates.today.toLocaleDateString()).toBe('12/1/2015');
});
it("anotherDay has a value of 1/1/2016", function() {
expect(dates.anotherDay.toLocaleDateString()).toBe('1/1/2016');
})
});
Here's a JSFiddle example of the issue.
How can I mock only today's date, and allow for new Date(yyyy, mm, dd)
to create the proper date object? I would expect that both tests in the fiddle pass, i.e. anotherDay
is set to 1/1/2016
and today
is set to 12/1/2015
.
Karma-Jasmine v 0.1.6.
I am writing a Jasmine unit test for a function in which dates are pared. I want to provide a fake date to be used for today's date. Therefore I am spying on the Date
method on the window object and returning a predefined date.
This works fine, but in the function I'm testing, I am also reading dates from a string and calling new Date(yyyy, mm, dd)
to turn them into dates. When this occurs, these values are replaced with the mock date I provided.
Here's an example:
var checkDate = function () {
return { today: new Date(), anotherDay: new Date(2016, 0, 1) }
};
var createDate = function (year, month, date) {
var overrideDate = new Date(year, month, date);
spyOn(window, 'Date').andCallFake(function () {
return overrideDate;
})
}
var dates;
describe("checkDate", function() {
beforeEach(function() {
createDate(2015, 11, 1);
dates = checkDate();
})
it("today has a value of 12/1/2015", function() {
expect(dates.today.toLocaleDateString()).toBe('12/1/2015');
});
it("anotherDay has a value of 1/1/2016", function() {
expect(dates.anotherDay.toLocaleDateString()).toBe('1/1/2016');
})
});
Here's a JSFiddle example of the issue.
How can I mock only today's date, and allow for new Date(yyyy, mm, dd)
to create the proper date object? I would expect that both tests in the fiddle pass, i.e. anotherDay
is set to 1/1/2016
and today
is set to 12/1/2015
.
Karma-Jasmine v 0.1.6.
Share Improve this question asked Jan 27, 2016 at 17:20 LefkaLefka 6832 gold badges7 silver badges22 bronze badges2 Answers
Reset to default 7It's best to use Jasmine Clock API
beforeEach(() => {
const fixedDate = new Date(2020, 0, 1);
jasmine.clock().install();
jasmine.clock().mockDate(fixedDate);
});
afterEach(() => {
jasmine.clock().uninstall();
});
src: https://stackoverflow./a/48574541
You can cache the window.Date
to use it when arguments are passed into your mock
var windowDate = window.Date;
spyOn(window, 'Date').andCallFake(function (year,month,day) {
if(year != undefined && month != undefined && day != undefined){
return new windowDate(year,month,day);
}
return overrideDate;
})
https://jsfiddle/n3Lf0g8p/1/
本文标签: javascriptMock only today39s date in Jasmine unit testStack Overflow
版权声明:本文标题:javascript - Mock only today's date in Jasmine unit test - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744064376a2527312.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论