Currently, I am conducting unit tests on my AngularJS client code and trying to decipher its meaning
var newdate = new Date(2013,6,29);
spyOn(Date.prototype, 'getTime').and.callFake(function() {
return newdate;
});
In this code snippet, we are mocking the getTime() method of the Date object. However, I am interested in mocking out the new Date() function instead. An example line of code that I need to test includes:
payload.created_at = new Date();
Unfortunately, I do not have access to payload.created_at. Hence, I aim to instruct Jasmine to replace any occurrence of new Date() with a specified date. I attempted the following, but it did not yield the desired result:
spyOn(Date.prototype, 'new Date').and.callFake(function() {
return newdate;
});
It became apparent to me that new Date is not a method of the Date object. Could someone please assist me in resolving this issue? Thank you.