← Back to Upcase

Capybara does not check checkboxes

(Acandael Acandael) #1

For my integration test, I want to check some checkboxes in a form.

But for some reason, Capybara doesn’t check the checkboxes. When I use ‘save_and_open_page’ to view the page in the browser while testing, the checkboxes are not checked.

my form looks like this:

<%= form_for [:admin, @research_project] do |f| %>
    <%= f.label :title %>
    <%= f.text_field :title %>
    <%= f.label :body %>
    <%= f.text_area :body %>
     <%= f.collection_check_boxes :research_theme_ids, ResearchTheme.all, :id, :title %>
  <%= f.submit "Add Research Project" %>
<% end %>

The rendered html looks like this:

<form accept-charset="UTF-8" action="/admin/research_projects" class="new_research_project" id="new_research_project" method="post"><div style="display:none"><input name="utf8" type="hidden" value="&#x2713;" /></div>
    <label for="research_project_title">Title</label>
    <input id="research_project_title" name="research_project[title]" type="text" />
    <label for="research_project_body">Body</label>
    <textarea id="research_project_body" name="research_project[body]">
     <input id="research_project_research_theme_ids_1"     name="research_project[research_theme_ids][]" type="checkbox" value="1" /><label      for="research_project_research_theme_ids_1">voluptas rem consectetur aut quas</label><input    id="research_project_research_theme_ids_2" name="research_project[research_theme_ids][]"     type="checkbox" value="2" /><label for="research_project_research_theme_ids_2">et non architecto voluptatem aliquam</label><input id="research_project_research_theme_ids_3" name="research_project[research_theme_ids][]" type="checkbox" value="3" /><label for="research_project_research_theme_ids_3">ab consequatur quia ducimus et</label><input name="research_project[research_theme_ids][]" type="hidden" value="" />
  <input name="commit" type="submit" value="Add Research Project" />

My integration test looks like this:

scenario "Admin adds research themes to research project" do
research_theme_1 = Fabricate(:research_theme)
research_theme_2 = Fabricate(:research_theme)
research_theme_3 = Fabricate(:research_theme)

click_link "Add Research Project"

expect(page).to have_css 'label', text: research_theme_1.title
expect(page).to have_css 'label', text: research_theme_2.title
expect(page).to have_css 'label', text: research_theme_3.title


click_button "Add Research Project"

expect(@research_project.research_themes.count).to eq(2)


I’m using Rails 4.1.0 and Capybara 2.3.0

Does anyone know how to solve this problem?

thanks for your help,


(Acandael Acandael) #2

Capybara doesn’t say it doesn’t find the checkboxes, it just doesn’t check them, so I wonder if this is a bug in Capybara 2.3.0, or if Capybara doesn’t play well Rails 4 collection_check_boxes

my repo is at: https://github.com/acandael/posplus-rails

the test is in:

the failing scenario is called: “Admin adds research themes to research project”

thanks for your help,