I have a form with a date_select which shows 3 selectboxes for day, month and year. I have a rspec integration test where I want to select today's date with capybara. So I have to split Date.today into a day, month and year. What's a good way to do this? I could do some string splits, but maybe there is a more sophisticated way to do?
Here you see the capybara code to select the date hard coded:
select("13", :from => "visit_visit_date_3i")
select("July", :from => "visit_visit_date_2i")
select("2012", :from => "visit_visit_date_1i")
There are methods for extracting date parts from a Date object. The date class also has some constant arrays that make it easy to convert things like months and days of the week to their English versions.
Thus:
Date.today.day
=> 13
Date::MONTHNAMES[Date.today.month]
=> "July"
Date.today.year
=> 2012
All this is in the docs for Date, which are located here .
You might consider using timecop to freeze the current date. This comes in handy later when testing whether other methods that deal with 'today' or 'one week from today'
before { Timecop.freeze(Date.parse("Jul 13 2012") }
after { Timecop.return }
Now you can select("13", :from => "visit_visit_date_3i")
and it will work for any date on which the test is run.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.