I have a method that uses DateTime.now to perform a search on some data, I want to test the method with various dates but I don't know how to stub DateTime.now nor can I get it working with Timecop ( if it even works like that ).
With time cop I tried
it 'has the correct amount if falls in the previous month' do
t = "25 May".to_datetime
Timecop.travel(t)
puts DateTime.now
expect(@employee.monthly_sales).to eq 150
end
when I run the spec I can see that puts DateTime.now gives 2015-05-25T01:00:00+01:00
but having the same puts DateTime.now within the method I'm testing outputs 2015-07-24T08:57:53+01:00
(todays date).
How can I accomplish this?
------------------update---------------------------------------------------
I was setting up the records (@employee, etc.) in a before(:all)
block which seems to have caused the problem. It only works when the setup is done after the Timecop do
block. Why is this the case?
Timecop.return
when you are done). Can you please paste the body of themonthly_sales
method? – Appurtenant