问题
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.
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?
Update:
I now have this code:
import * as moment from 'moment';
describe('Login & Home Page', () => {
beforeEach(() => {
let date = moment('08-03-2017');
moment = () => { return date };
})
// ...
回答1:
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());
来源:https://stackoverflow.com/questions/44545638/mocking-the-date-in-protractor