gpt4 book ai didi

angular - 在 Protractor 中模拟日期

转载 作者:搜寻专家 更新时间:2023-10-30 21:51:04 25 4
gpt4 key购买 nike

我正在使用 Protractor 为我的 Angular (4) 应用程序编写一套端到端测试。我的后端配置为连接到生产数据库的精确副本,但填充了虚拟数据。

我前端的很大一部分是正确显示历史数据。目前,有一段时间有我要显示的数据。然而,显然在一周的时间里,我的“每周” View 将所有数据显示为 0。

是否有可能欺骗 Protractor 认为日期在我播种虚拟数据的时间段内,所以显示的数据是可预测的?

更新:

我现在有这段代码:

import * as moment from 'moment';

describe('Login & Home Page', () => {
beforeEach(() => {
let date = moment('08-03-2017');
moment = () => { return date };
})
// ...

最佳答案

如果您在代码中使用 Date 构造函数,您可以重写它。

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

取决于您如何在代码中获取日期,但是它会覆盖相同的内容以返回所需的日期。

如果您使用的是 moment,则重写您正在使用的完全相同的函数。

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'))
});

嗯,我想 jasmine.clock.mockDate() 才是真正的方法

var today = moment('2015-10-19').toDate();
jasmine.clock().mockDate(today);
expect(moment().valueOf()).toEqual(today.valueOf());

关于angular - 在 Protractor 中模拟日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44545638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com