jflores
jflores

Reputation: 483

Capybara::ElementNotFound on Dropdown Collection Select using Rspec

I've been banging my head against the wall trying to figure out why this test isn't passing in Rspec. It works in the browser.

I have a Course form that belongs_to a Grade object. In the Course form, there is a select box that allows the user to select Grade:

<%= form_for [current_user, @course] do |course| %>
...
<%= course.label :grade_id, "What age are the students?" %>
<%= course.collection_select(:grade_id, Grade.all, :id, :grade_level, options ={:prompt => "Select Grade"})  %>

My test in Rspec looks like this:

describe "A workign form" do
  before do
    sign_in_via_form #signs the user in
    visit new_user_course_path(@user) #references @user, defined in Helper
  end
let(:course){@user.courses}

  context "With valid information" do
    it "adds a course" do
      expect {
        fill_in 'course_name', with:'Course Name'
        select 'Fall', from: 'course_course_semester'
        select '2012', from: 'course_course_year'
        select 'Grade 5', from: 'course_grade_id'
        fill_in 'course_summary', with: 'Perfunctory Summary'
        fill_in 'course_objectives_attributes_0_objective', with: "an objective"
        click_button "submit"
     }.to change(course, :count).by(1)
    end
  end
...#other tests
end #describe block

The HTML generated in my form looks like this:

<label for="course_grade_id">What age are the students?</label>
<select id="course_grade_id" name="course[grade_id]"><option value="">Select Grade</option>
    <option value="1">Kindergarten</option>
    <option value="2">Grade 1</option>
    <option value="3">Grade 2</option>
    <option value="4">Grade 3</option>
    <option value="5">Grade 4</option>
    <option value="6">Grade 5</option>
    <option value="7">Grade 6</option>
    <option value="8">Grade 7</option>
    <option value="9">Grade 8</option>
    <option value="10">Grade 9</option>
    <option value="11">Grade 10</option>
    <option value="12">Grade 11</option>
    <option value="13">Grade 12</option>
    <option value="14">High School</option>
</select>

Let me know if there's other code needed; I'm more than happy to provide it. My other select boxes are working, but they're also part of the model with Arrays driving the contents. In this case, though, an associated model is driving the content. I'm not sure if that matters, there it is if it does.

Upvotes: 0

Views: 2144

Answers (1)

dimuch
dimuch

Reputation: 12818

The data for the dropdown comes from the database. Rails uses separate DB for test, and its tables are empty by default. So you need to populate the grades table in order to have some options in the dropdown.

With FactoryGirl it can looks like

FactoryGirl.define do
  factory :grade do
    sequence(:grade_level) { |n| "Grade #{n}" }
  end
end

And the test

describe "A workign form" do
  before do
    sign_in_via_form #signs the user in
    FactoryGirl.create_list(:grade, 14) # fill the grades table before visit the page
    visit new_user_course_path(@user) #references @user, defined in Helper
  end
  ...

Upvotes: 3

Related Questions