mardi 2 juin 2015

jasmine spyOn on javascript new Date

I am unit testing the my client code in angulajs and i understand this code means

 var newdate = new Date(2013,6,29);
    spyOn(Date.prototype, 'getTime').and.callFake(function() {
          return newdate;
        });

we mockout the getTime() methode of the Date object. but i want to mock out the new Date() instead. for example the code i want to test contains this line

payload.created_at = new Date();

I dont have access to payload.created_at. so i want to tell jasmine that whenever you see new Date(), replace that with a given date i give you. So i was thinking of something like but it doesnt work.

spyOn(Date.prototype, 'new Date').and.callFake(function() {
          return newdate;
        });

but new Date is not a method of the Date. Please can someone help me figure this out? thanks

Aucun commentaire:

Enregistrer un commentaire