2013-05-22 4 views
0

Rspec/TDD 초보자입니다. 나는 실패한 스펙을 가지고 있는데 이유를 모른다. 모든 것이 브라우저에서 작동합니다. 나는 페이지 당 25 개의 항목을 기본값으로하는 페이지 매김에 Kaminari를 사용하고 있습니다.색인의 목록 항목을 설명하는 Rspec 테스트가

사양 :

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 

실패 : 나는 25를 만들기 위해 그것을 말했을 때

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

이 왜 항목 번호 (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 

루비 1.9.3p429, 3.2.13 레일, 레일 RSpec에 2.13.1, 2.1 삐 .0, kaminari 0.14.1, faker 1.0.1, factory_girl_rails 4.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 
+0

두 블록을 작업 전 블록 안에 넣습니다. 감사! 원래 구문이 작동하지 않는 이유에 대한 아이디어는 무엇입니까? @lis2 – Dan

+0

첫 번째 예에서는 questions_path를 방문한 후에 질문이 작성되므로 페이지가 렌더링 될 때 질문이 없습니다. 두 번째 질문에서는 카피 바라가 셀렉터를 찾을 수 있도록 페이지를 방문하기 전에 질문이 만들어집니다 (올바르게). – ian