2012-12-13 33 views
0

我一直在撞墙我试图找出为什么这个测试没有通过Rspec。它在浏览器中工作。Capybara :: ElementNotFound在下拉选择使用Rspec

我有一门课程表,属于成绩对象。在课程的形式,有一个选择框,让用户选择等级:

<%= 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"}) %> 

我在Rspec的测试看起来是这样的:

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 

在我的形式生成的HTML如下:

<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> 

让我知道是否有其他代码需要;我非常乐意提供它。我的其他选择框正在工作,但它们也是阵列驱动内容的模型的一部分。但是,在这种情况下,相关模型正在驱动内容。我不确定这是否重要,如果有的话。

+0

你有测试前填充的成绩? – dimuch

+0

我不确定......成绩在单独的表格中,成绩,这是collection_select中的值来自哪里。我的考试中没有明确的成绩。我应该添加他们的地方?哪里? – jflores

回答

3

下拉列表中的数据来自数据库。 Rails使用单独的数据库进行测试,默认情况下它的表为空。所以你需要填写成绩表,以便在下拉列表中有一些选项。

随着FactoryGirl它可以看起来像

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

而且测试

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 
    ... 
+0

非常感谢您指点我这个方向;我没有意识到数据库是如何在测试环境中工作的,并且在实现这个过程中查看日志非常有帮助。 – jflores

+1

谨慎的一句话(对于那些可能会看到解决同样问题的人来说),这个方法会不断地通过你的每个例子递增序列。为了重置每个测试的序列,我在(:each)块之后使用了FactoryGirl.reload。根据这篇文章:http://stackoverflow.com/questions/3359168/how-can-i-reset-a-factory-girl-sequence这是一个“反模式”,但它现在为我工作。 – jflores

+0

jflores建议使用'FactoryGirl.reload'+1。在一个侧面说明中,帮助我解决这个时间问题的一个工具是'launchy'宝石。您可以在测试执行的各个部分抛弃'save_and_open_page',并查看视图的collection_select何时/是否已填充。 – wintondeshong

相关问题