【问题标题】:Rspec test describing listing items on index failing描述索引失败的列表项的 Rspec 测试
【发布时间】:2013-05-22 02:30:41
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    你能确保你在访问之前创建工厂吗?

    也许像代替

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

    那个

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

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

    puts page.inspect
    

    【讨论】:

    • 将两个语句放在一个 before 块中起作用。谢谢!关于为什么原始语法不起作用的任何想法? @lis2
    • 在第一个示例中,问题是在访问 questions_path 之后创建的,因此在页面呈现时问题不存在。第二,在访问页面之前(正确)创建问题,以便水豚能够找到选择器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多