在量角器中模拟日期 [英] Mocking the date in Protractor

查看:106
本文介绍了在量角器中模拟日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用量角器为我的angular(4)应用程序编写一套端到端测试。我的后端被配置为连接到生产数据库的精确副本,但填充有伪数据。

I am writing a suite of end to end tests for my angular (4) application, using protractor. My backend is configured to connect to an exact replica of the production database, but filled with dummy data.

我前端的很大一部分正确显示了历史数据。当前,有一段时间要显示我要显示的数据。但是,很明显,在一个星期的时间里,我的每周视图会将所有数据显示为0。

A big part of my front end, is correctly displaying historic data. Currently, there is a certain period which has the data I want to display. However, obviously in a weeks time, my "Weekly" view will display all data as 0.

是否有可能诱使量角器认为日期在我所指的时期内种子虚拟数据,因此显示的数据是可预测的?

Is it possible to trick protractor into thinking the date is within the period I seeded the dummy data, so the displayed data is predictable?

我现在有了此代码:

import * as moment from 'moment';

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


推荐答案

如果在代码中使用 Date 构造函数,则可以覆盖

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






嗯,我想 jasmine.clock.mockDate()是实际的方法

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

这篇关于在量角器中模拟日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆