blundin
blundin

Reputation: 1643

Capybara 2.1.0 & Rspec 2 'have_selector' test is broken

I have a looping 'have_selector' test that is not passing, but a more direct test using Capybara.string does. Can anyone explain this?

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

When this test is executed the commented out 'expect()' test does not pass, but the test below it does. The failure message is:

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

Upvotes: 0

Views: 688

Answers (1)

vee
vee

Reputation: 38645

The reason for the error is because you're calling has_selector? on String class. has_selector? is from Capybara::Node::Matchers module.

To fix the issue, update the example with the following:

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

Upvotes: 1

Related Questions