kucaahbe / rspec-html-matchers

Old school have_tag, with_tag(and more) matchers for rspec 3 (Nokogiri powered)
http://rubygems.org/gems/rspec-html-matchers
MIT License
199 stars 90 forks source link

have_tag('option') and with_option #39

Closed christophermlne closed 9 years ago

christophermlne commented 9 years ago

edit: For anyone struggling with this in the future — this issue is resolved. See my note below. ~~

I'm experiencing surprising behaviour while using the option tag helpers, either as with_option or have_tag('option')

have_tag('option'):

Failure/Error: expect(@rendered_form).to have_tag('option', :with => {:value => 'country-1', :text => 'Canada'})
       expected following:
       <form action="http://www.example.com/submissions" method="post"><label for="countries">Select a country</label><br /><select id="countries" name="countries"><option selected value="country-1">Canada</option><option value="country-2">Zimbabwe</option></select></form>
       to have at least 1 element matching "option[value='country-1'][text='Canada']", found 0.

with_option:

Failure/Error: with_option(:with => {:value => 'country-1', :text => 'Canada'})
       "{:with=>{:value=>"country-1", :text=>"Canada"}}" expected within "option" in following template:
       <select id="countries" name="countries"><option selected value="country-1">Canada</option>
       <option value="country-2">Zimbabwe</option></select>
christophermlne commented 9 years ago

I'd have no issue submitting a PR to fix if you verify that it is indeed an issue and not something I'm doing wrong.

kucaahbe commented 9 years ago

@christophermlne looks like an issue to me

christophermlne commented 9 years ago

ok thx. i'll take a look.

christophermlne commented 9 years ago

Ok turns out there isn't a bug. It's more of a documentation issue.

After running examining the specs for the with_option() matcher, it turns out I had passed in the option tag's 'value' as the selector instead of its 'text'.

For anyone reading this in the future:

With this html fragment, if you want to make assertions about options tags within a select:

<form id="my-special-form" action="http://www.example.com/submissions" method="post">
  <label for="countries">Select a country</label><br>
  <select id="countries" name="countries">
       <option value="country-1">Canada</option>
       <option selected value="country-2">Zimbabwe</option>
  </select>
</form>

Don't do this (bad):

expect(@rendered_form).to have_tag('option',
            :with => {:value => 'country-2', :text => 'Zimbabwe', :selected => 'selected'})

Do something like this instead (good):

expect(@rendered_form).to have_tag('form') do
  with_select('countries') do
    with_option('Canada')
    with_option('Zimbabwe', :selected => true)
  end
end

Notice how the with_select is matching against the select tag's 'id' while the with_options are matching against the text contained within the tag.

kucaahbe commented 9 years ago

thanks @christophermlne !