I have a line like this in my code
new Date().getFullYear();
and I need it to always return the same year.
Is there any way to do this with Jasmine ?
Thanks.
In JavaScript you can simply overwrite the function in your test setup:
Date.prototype.getFullYear = function(){return 2012}
You could also use a jasmine spy:
spyOn(Date, 'getFullYear').andReturn(2012)
Another way is to insert the date into your function you wanna test. Which is btw. the best way to write testable code. Dont create new instances in your functions, cause you always start to test not only your code but also this instances.