oFca
oFca

Reputation: 2830

How to test selecting dates with rspec?

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?

Upvotes: 1

Views: 4030

Answers (2)

Kevin Bedell
Kevin Bedell

Reputation: 13404

Rails Form Helpers break date select fields down into a set of data parts -- potentially naming them from 1i to 6i -- 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:

 <%= f.date_select(:target_date,
                    :default => Date.today,         
                    :order => [:day, :month, :year], :start_year => Date.today.year, 
                    :end_year => Date.today.year + 3)
%>

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:

select '2011/01/01', :from => 'Show Date'

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

Upvotes: 3

Suborx
Suborx

Reputation: 3669

Try this

select '28', :from => 'task[target_date]'

or give the select some special id

<%= f.date_select(:target_date,
                :default => Date.today,         
                :order => [:day, :month, :year], 
                :start_year => Date.today.year, 
                :end_year => Date.today.year + 3,
                :id => 'task_target_date') %>

and then try

select '28', :from => 'task_target_date'

Upvotes: 1

Related Questions