Mocking the date in Protractor

蓝咒 提交于 2019-12-06 09:55:50

If you are using Date constructor in your code you can override the same.

var d = new Date(2017, 5, 14);
Date = function(){return d;};

Depends on how you are getting date in your code, however it is override the same to return desired dates.

If you are using moment then override the exact same function(s) you are using.

let date = moment(2017, 6, 14);
moment = () => {return date};

 describe('Login & Home Page', () => {
    let realMoment = moment;
    let moment = function(){
        this.prototype = realMoment.prototype;
        return realMoment('08-03-2017', 'MM-DD-YYYY'); 
    };
    beforeEach(() => {
    });
    it('expect moment(\'asd\') to return 08-03-2017', () => {
       expect(moment('asd').format('MM-DD-YYYY')).toBe('08-03-2017'))
    });

Well, I guess jasmine.clock.mockDate() is the actual way to go

var today = moment('2015-10-19').toDate();
jasmine.clock().mockDate(today);
expect(moment().valueOf()).toEqual(today.valueOf());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!