Question

Rspec/TDD beginner. I have a spec that is failing and I don't know why. Everything works in the browser as it should. I am using Kaminari for pagination, which defaults to 25 items per page.

spec:

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

failure:

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

Why is it failing on item number 33 when I told it to make 25?

factory:

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

view:

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

controller:

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

Was it helpful?

Solution

can you ensure that you are creating factories before visit ?

Maybe something like instead of

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

that

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

If it will not help maybe you can display content of page?

puts page.inspect
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top