Rspec test failure: Static pages About page should have the title 'About Us' Hartl Ch. 3
-
11-12-2019 - |
Question
I am working through Michael Hartl's Ruby on Rails tutorial, and I am doing the Chapter 3 exercises. Can someone please explain why this test is failing?
I am getting the failure
rspec ./spec/requests/static_pages_spec.rb:39 #
Static pages About page should have the title 'About Us'
Controller
class StaticPagesController < ApplicationController
def home
end
def help
end
def about
end
def Contact
end
end
About.html.erb
<!DOCTYPE html>
<html>
<head>
<title>Ruby on Rails Tutorial Sample App | About Us</title>
</head>
<body>
<h1>About Us</h1>
Spec.rb
describe "About page" do
it "should have the h1 'About Us'" do
visit '/static_pages/about'
page.should have_selector('h1', :text => 'About Us')
end
it "should have the title 'About Us'" do
visit '/static_pages/about'
page.should have_selector('title',
:text => "Ruby on Rails Tutorial Sample App | About Us")
end
end
Routes.rb
SampleApp::Application.routes.draw do
get "static_pages/home"
get "static_pages/help"
get "static_pages/about"
get "static_pages/Contact"
end
Solution
Try
page.should have_xpath("//title", :text => "About Us")
OTHER TIPS
If you're following MHartl's tutorial and you're using the gem 'Capybara', I can confirm that changing your ':text => ...' to ':content => ...' will make your tests pass. Thanks to @Kuo Jimmy.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow