1

I am trying to test the title of my static pages in rails. I'm using Capybara 2.4.4 and rspec 3.

My test looks like the following static_pages_controller_spec.rb

require 'spec_helper'
require 'rails_helper'
describe StaticPagesController do
  describe "GET 'Index'" do
    it "should be successful" do 
      visit root_path
      response.should be_success
    end

    it "should have the right title" do
      visit root_path
      expect(page).to have_selector('title', 
                     :text => "Index",
                     :visible => false)
    end 
  end
end

The page does have the correct title set. The error I'm getting says the following

Failure/Error: expect(page).to have_selector('title',
       expected to find css "title" with text "Index" but there were no matches
Questifer
  • 1,113
  • 3
  • 18
  • 48
  • you should expect page(page).to have_content instead – Joel Dec 30 '14 at 23:30
  • Perhaps post the view code? – roob Dec 31 '14 at 00:51
  • http://stackoverflow.com/questions/13573525/rspec-capybara-2-0-tripping-up-my-have-selector-tests This should have your answer. – trueinViso Dec 31 '14 at 01:01
  • @trueinViso I tried the most updated answer and am receiving expected "" to include "Index" error – Questifer Dec 31 '14 at 14:43
  • You tried `expect(page).to have_title "Index"`? And you have `Index<\title>` on the page you are testing? And just to make sure you did `save_and_open_page` in your test and inspected the element to make sure that html is there? – trueinViso Dec 31 '14 at 16:16
  • I noticed you put visit root_path, is root path the path to this controller method/view? – trueinViso Dec 31 '14 at 16:23

1 Answers1

0

I spent quite a few hours trying to figure it out as well.

Ultimately what worked was this

Install capybara as a gem

group :test do
  gem 'rspec'
  gem 'capybara'
end

and run bundle install

Add the following to the top of spec/spec_helper.rb

require 'capybara/rails'
require 'capybara/rspec'

Then inside the RSpec.configure, add the config.include Capybara::DSL

RSpec.configure do |config|
  .
  .
  .
  config.include Capybara::DSL
end

Then in your pages_controller_spec.rb, modify to use as follows.

Notice that I have included Capybara.ignore_hidden_elements = false

describe PagesController do
  render_views
  Capybara.ignore_hidden_elements = false

  describe "GET 'home'" do
    it "should be successful" do
      get 'home'
      response.should be_success
    end
    it "should have the right title" do
      get 'home'
      response.body.should have_xpath("//title",:text => "ROR")
    end
  end

end

Check my repository if you want see anything else

The following cheatsheet should come handy as well
https://gist.github.com/them0nk/2166525

Ranhiru Jude Cooray
  • 19,542
  • 20
  • 83
  • 128
  • My apologies for not coming back to this sooner. I just tried to implement your solution and it still isn't working. I am receiving "expected to find xpath "//title" with text "TITLE" but there were no matches". – Questifer Feb 18 '15 at 21:48