This is from Ruby on Rails Tutorial by Michael Hartl 2nd edition. The 3rd (and currently latest) edition does not test with RSpec so I decided to use this book.
I've read users' workarounds and know there are different ways to write the test to make it work but I want to use have_selector to keep the code consistent. Any explanation/advice would be very helpful. I do not understand why the below test passes for the h1 element and not the title element:
spec.rb:
describe "Static pages" do
describe "Home page" do
it "should have the h1 'Sample App'" do
visit '/static_pages/home'
expect(page).to have_selector('h1', :text => 'Sample App')
end
it "should have the title 'Home'" do
visit '/static_pages/home'
expect(page).to have_selector('title',
:text => "Ruby on Rails Tutorial Sample App | Home")
end
end
home.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Ruby on Rails Tutorial Sample App | Home</title>
</head>
<body>
<h1>Sample App</h1>
</body>
</html>
Try:
expect(page).to have_selector('title',
:text => "Ruby on Rails Tutorial Sample App | Home",
:visible => false)
Since the title tag is in <head>
element, it is considered hidden. Specifying :visible => false
includes those tags for consideration in the have_selector matcher.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With