← Back to Upcase

Capybara not selecting option


(Acandael Acandael) #1

Hi, I have a feature test to test the addition of a ‘publication’ object. For some resean Capybara doesn’t select the research project from the select box.

My feature test looks like this:

background do
  @publication = Fabricate(:publication)  
  admin = Fabricate(:admin)
  sign_in(admin)
  visit admin_publications_path
end

 scenario 'admin adds a new publication' do
   @research_project = Fabricate(:research_project)
   expect{
     find("input[@value='Add Publication']").click
     fill_in 'Title', with: @publication.title
     fill_in 'Reference', with: @publication.reference
     select @research_project.title, :from => 'publication_research_project_id'
     click_button 'Add Publication'
   }.to change(Publication, :count).by(1)
   expect(page).to have_css 'p', text: "You successfully added a publication"
   expect(@publication.research_project.title).to eq(@research_project.title)
end

My form looks like this:

<%= form_for [:admin, @publication] do |f| %>
  <div>
    <%= f.label :title %>
    <%= f.text_field :title %>
  </div>
  <div>
    <%= f.label :reference %>
    <%= f.text_area :reference %>
  </div>
  <div>
    <%= f.label :research_project %>
    <%= f.collection_select :research_project_id, ResearchProject.all, :id, :title, prompt: true %>
  </div>
  <% if @publication.new_record? %>
    <%= f.submit "Add Publication" %>
  <% else %>
    <%= f.submit "Update Publication" %>
  <% end %>
  <%= link_to 'Cancel', admin_publications_path %>
<% end %>

Does anyone have a clue what the problem could be?

Thanks for your help,

Anthony


(Frank West) #2

I am not familiar with Fabricator, but it appears that you are not referencing the correct instance of publication in your expect statement. You create an instance in your background block and then you create another instance in your scenario. At this point you may have two publications. Maybe changing the expect to the following would work?

expect(Publication.last.research_project.title).to eq(@research_project.title)

(Tom Ridge) #3

Hey @acandael_acanda , also not familiar with Fabricator, but what happens when you output the result of ResearchProject.all in your spec i.e

...
puts ResearchProject.all
select @research_project..

(Acandael Acandael) #4

Hi Tom,

when I check the form with Launchy’s ‘save_and_open_page’ command, the research project is available as an option in the select dropdown:

<select id="publication_research_project_id" name="publication[research_project_id]">
  <option value="">Please select</option>
  <option value="1623">autem soluta quaerat aut nesciunt</option>
</select>

But for som resean Capybara doesn’t seem to select it.

greetings,

Anthony


(Acandael Acandael) #5

Hi Frank, your absolutely right. I was forgetting that I already had a publication object in the database (created in the background proces) and that I’m actually creating a second publication item. So changing my expectation to:

expect(Publication.last.research_project.title).to eq(@research_project.title)

made the test pass.

thanks a lot,

Anthony