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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.