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.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In JavaScript you can simply overwrite the function in your test setup:
You could also use a jasmine spy:
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.