I'm writing a spec for a controller in Rails 3 project using RSpec and Capybara, and I want to select current date from a select box. I tried:
select Date.today, :from => 'Date of birth'
but the spec fails and I get error:
Failure/Error: select Date.today, :from => 'Date of birth' NoMethodError: undefined method `to_xpath' for Mon, 18 Jul 2011:Date
How to fix it?
P.S. In view file I use simple_form_for tag and the select box is generated by code:
f.input :date_of_birth
Given the following Formtastic code renders Rails default date selector:
In your spec, break the date into separate calls to each individual select tag:
I don't like that this code is so aware of the HTML, but it does work with the versions of gems at this time.
Gems:
The following worked for me, using a date_field:
It looks like this one has been sufficiently covered, but see Capybara's docs for an official answer. You can select by name, id, or label text.
with credit to Markus Hartmair for an excellent solution, I prefer to use labels as selectors because of improved readability. So my version of his helper module is:
call it like this:
For Rails 4, in case somebody gets to this question without being limited to Rails 3.
You can of course extract this to a helper and make it dynamic.