How to test the following code with integration rspec test (I’m also using capybara) ?
1 <%= form_for [@project, @task] do |f| %>
17 <p>
18 <%= f.date_select(:target_date,
19 :default => Date.today,
20 :order => [:day, :month, :year], :start_year => Date.today.year,
21 :end_year => Date.today.year + 3) %>
22 </p>
23
24 <%= f.submit %>
25
26 <% end %>
So far I have the following piece of code…
select '28', :from => :day
…but it says > cannot select option, no select box with id, name, or label ‘day’ found
EDIT: Upon looking at the HTML output of the page I noticed Rails automatically adds id’s to day, month and year field…
The output for day for example looks like this…
<select id="show_date_3i" name="show[date(3i)]"><option value="1">1</option>
But then again when I do this…
select '28', :from => :show_date_3i
… it says >>> cannot select option, no select box with id, name, or label ‘show_date_3i’ found… Why is that?
Rails Form Helpers break date select fields down into a set of data parts — potentially naming them from
1ito6i— they represent the year, month, date, hour, minute and second of the datatime.For dates, only the pieces for year, month and date are used.
So given your form helper tag:
I’d recommend you first work to understand exaclty how the html dates get rendered in the select boxes, then use the capybara matchers to select them.
So, for example, if your date select was ‘2011/01/01’ and your select was ‘Show Date’, you’d write:
See this related question:
How to select date from a select box using Capybara in Rails 3?
And the capybara matchers doc at ruby-doc.info:
http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Matchers