Rspec测试描述索引失败的列表项

时间:2013-05-22 02:30:41

标签: ruby-on-rails-3 rspec capybara factory-bot kaminari

Rspec / TDD初学者。我有一个失败的规格,我不知道为什么。一切都在浏览器中工作。我使用Kaminari进行分页,每页默认为25个项目。

规格:

describe "Question Pages" do

    subject { page }

    describe "index page" do

        before { visit questions_path }
        before { 25.times { FactoryGirl.create(:question) } }
        after { Question.delete_all }
        it { should have_selector('h1', 'All Questions') }

        it "should list the questions" do
            Question.page(1).each do |question|
                page.should have_link(question.title, href: question_path(question))
            end
        end
    end
end

故障:

 1) Question Pages index page should list the questions
     Failure/Error: page.should have_link(question.title, href: question_path(question))
     Capybara::ExpectationNotMet:
       expected to find link "Lorem Ipsum 33" but there were no matches
     # ./spec/features/question_pages_spec.rb:17:in `block (4 levels) in <top (required)>'
     # ./spec/features/question_pages_spec.rb:16:in `block (3 levels) in <top (required)>'

当我告诉它制作25时,为什么第33项失败?

工厂:

FactoryGirl.define do 
    factory :question do
        sequence(:title) { |i| "Lorem Ipsum #{i}" }
        body "Dolor sit amet"
        passed false
    end
end

视图:

<h1>All Questions</h1>
 <%= paginate @questions %>
 <ul class="questions">
     <% @questions.each do |question| %>
          <li>
             <section class="question_<%= question.id %> clearfix">
                 <h2><%= link_to truncate(question.title, length: 62), question_path(question) %></h2>
                 <p><%= truncate(question.body, length: 70) %></p>

          </li>
      <% end %>
 </ul>

控制器:

class QuestionsController < ApplicationController
    def index
        @questions = Question.page(params[:page])
    end
end

ruby​​ 1.9.3p429,rails 3.2.13,rspec-rails 2.13.1,capybara 2.1.0,kaminari 0.14.1,faker 1.0.1,factory_girl_rails 4.1.0

1 个答案:

答案 0 :(得分:0)

您可以确保在访问之前创建工厂吗?

可能代替

before { visit questions_path }
before { 25.times { FactoryGirl.create(:question) } }

before do 
  25.times { FactoryGirl.create(:question) }
  visit questions_path 
end

如果它没有帮助,你可以显示页面内容吗?

puts page.inspect