Search code examples
ruby-on-rails-3rspec2rspec-railswebrat

Rails rspec/webrat - Can't see checkboxes


I'm running into an issue where webrat can't see checkboxes. I have some code like this (haml):

    = form_for(@advanced_search, :url => searches_path, :method => 'get', :html => {:class => 'well', :id => 'advanced-search'}) do |f|
Availability
          - @advanced_search.availability_types.each do |avail_type|
            = check_box_tag "search[availability_types][kind][#{avail_type.id}]", avail_type.id, checked = true, :class => 'checkbox'
            = avail_type.kind

Which produces this: checkboxes

It's all valid html, and the functionality works. However, when I go to test it, I don't see any of the checkboxes in the response.

Example test:

it "should have checkboxes" do
  get :index
  response.should have_selector("form") do |node|
    node.should have_selector('input', :type => 'checkbox')
  end
end

Output:

    expected following output to contain a <input type='checkbox'/> tag:
Availability
<br><input class="btn" name="commit" type="submit" value="Search">

The checkboxes don't show up and I'm not sure why. I thought that marking them as checked might do it because of the html funkyness with checkboxes, but it didn't seem to have any effect.

Edit 1 What I do know, if I do this:

Availability
      - @advanced_search.availability_types.each do |avail_type|
        = check_box_tag "search[availability_types][kind][#{avail_type.id}]", avail_type.id, checked = true, :class => 'checkbox'
        = avail_type.kind
      = check_box_tag "search[availability_types][kind][8]", 8, checked = true, :class => 'checkbox'

The non-dynamically generated checkbox shows up fine:

Availability
<input checked class="checkbox" id="search_availability_types_kind_8" name="search[availability_types][kind][8]" type="checkbox" value="8">

Edit 2

I was missing the data in the test database... Doh. I added these creates before and now my checkboxes show up.

before(:each) do
  AvailabilityType.create!({:kind => 'Full-time'})
  AvailabilityType.create!({:kind => 'Part-time'})
  AvailabilityType.create!({:kind => 'No Availability'})
end

Solution

  • availability_types appears to be loaded from an ActiveRecord table, correct? If so, I think you may not have the data loaded into that table so that when you are iterating over the types of availability_types, you don't even enter the block.

    Do you have spec/fixtures/availability_types.yml loaded with the data? Another option is to mock that data for that test or to use a factory or raw AR creation to create the data for the test.