Create a Date object for a specific day
Say you have some date-specific functionality, and you want to test for a specific day of the week.
Date#commercial
is what you're looking for! It will create a Date object for you based on the year,week, and day that you give it.
require 'date'
# Wednesday (3) of week 1 of year 2023
Date.commercial(2023, 1, 3)
In Rails we can take this a step further, for example, to get Friday of this week:
Date.commercial(Date.current.year, Date.current.cweek, 5)
In your testing you can simply make use of the Rails TimeHelpers to travel to that specific date you need:
next_friday = Date.commercial(Date.current.year, Date.current.cweek + 1, 5)
travel_to next_friday do
# Friday specific code
end
Tweet