【问题标题】:Capybara 2.1.0 & Rspec 2 'have_selector' test is brokenCapybara 2.1.0 & Rspec 2 'have_selector' 测试被破坏
【发布时间】:2014-05-28 11:55:45
【问题描述】:

我有一个循环的“have_selector”测试没有通过,但是使用 Capybara.string 的更直接的测试可以。谁能解释一下?

require 'spec_helper'

describe "Item pages" do

  subject { page }

  describe "Item list" do
    let(:user) { FactoryGirl.create(:user) }

    before do
      FactoryGirl.create(:item, user: user, description: "Testing")
      FactoryGirl.create(:item, user: user, description: "Testing 2")
      sign_in user
      visit items_path
    end

    it { should have_link("Logout") }

    it "should render the user's list of items" do
        user.items.each do |item|
          #expect(page).to have_selector("li##{item.id}", text: item.description)
          Capybara.string(page.body).has_selector?("li##{item.id}", text: item.description)
        end
      end
  end
end

执行此测试时,注释掉的“expect()”测试未通过,但它下面的测试通过。失败信息是:

Failure/Error: expect(page.body).to have_selector("li##{item.id}", text: item.description)
     NoMethodError:
       undefined method `has_selector?' for #<String:0x007fe2a9782dd0>

【问题讨论】:

    标签: ruby-on-rails ruby rspec capybara


    【解决方案1】:

    错误的原因是因为您在 String 类上调用 has_selector?has_selector? 来自 Capybara::Node::Matchers 模块。

    要解决此问题,请使用以下内容更新示例:

    it "should render the user's list of items" do
      user.items.each do |item|
        #expect(page).to have_selector("li##{item.id}", text: item.description)
        page.has_selector?("li##{item.id}", text: item.description)
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-28
      • 1970-01-01
      • 2015-04-07
      • 1970-01-01
      相关资源
      最近更新 更多