最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Mock only today's date in Jasmine unit test - Stack Overflow

programmeradmin0浏览0评论

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 badges
Add a ment  | 

2 Answers 2

Reset to default 7

It'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/

发布评论

评论列表(0)

  1. 暂无评论