Gandalf StormCrow
Gandalf StormCrow

Reputation: 26212

Testing with capybara and rspec

I'm currently writing fairly simple test case(still learning). I'm asserting whether about page has about h1.

This is my test :

describe "about verification page" do
  before { visit about_path }
  it { should have_selector('h1', text: 'About') }
end

My test fails with this reason :

Failure/Error: it { should have_selector('h1', text: 'About') }
expected #has_selector?("h1", {:text=>"About"}) to return true, got false

But when I go to my about page and type $('h1') I get About and I can see the text in browser.

What am I doing wrong with this? Or how can I get a value of h1 so I can print it and see what am I actually getting?

Upvotes: 1

Views: 1309

Answers (2)

Billy Chan
Billy Chan

Reputation: 24815

When you use it in one line flavor, you need to have a subject otherwise Rspec won't know what is the assertion.

Either of the following will work.

One line style

describe "about verification page" do
  before { visit about_path }
  subject { page }
  it { should have_selector('h1', text: 'About') }
end

Normal style

describe "about verification page" do
  before { visit about_path }

  it "should have selector h1" do
    expect(page).to have_selector('h1', text: 'about')
  end
end

Upvotes: 2

nikolayp
nikolayp

Reputation: 17949

or

describe "about verification page" do
  before { visit about_path }
  it 'works' do
    page.should have_selector('h1', text: 'About') 
  end
end

Upvotes: 0

Related Questions