RoR , trouble with full_title - ruby-on-rails

I am on Chapter 4 of Michael Hartl's RoR tutorial, and having some troubles with rspec and using the full_title helper function. In part 4.1 of the tutorial, I have the helper code as follows.
app/helpers/application_helper.rb
module ApplicationHelper
# Returns the full title on a per-page basis.
def full_title(page_title)
base_title = "Ruby on Rails Tutorial Sample App"
if page_title.empty?
base_title
else
"#{base_title} | #{page_title}"
end
end
end
app/views/layouts/application.html.erb
<!DOCTYPE html>
<html>
<head>
<title><%= full_title(yield(:title)) %></title>
<%= stylesheet_link_tag "application", media: "all",
"data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
spec/requests/static_pages_spec.rb
require 'spec_helper'
describe "Static pages" do
let(:base_title) {"Ruby on Rails Tutorial Sample App"}
describe "Home page" do
it "should have the content 'Sample App'" do
visit '/static_pages/home'
expect(page).to have_content('Sample App')
end
it "should have the base title" do
visit '/static_pages/home'
expect(page).to have_title("Ruby on Rails Tutorial Sample App")
end
it "should not have a custom page title" do
visit '/static_pages/home'
expect(page).not_to have_title('| Home')
end
end
end
app/views/static_pages/home.html.erb
<h1>Sample App</h1>
<p>this is the home page for the Ruby on Rails Tutorial
sample application.</p>
And when I try to run test I get this:
gvyntyk#gvyntyk-r60:~/rails_projects/sample_app$ rspec spec/requests/static_pages_spec.rb
FFF
Failures:
1) Static pages Home page should not have a custom page title
Failure/Error: visit '/static_pages/home'
ActionView::Template::Error:
undefined local variable or method `base_title' for #<#<Class:0xb0ea7f8>:0xb0ea1cc>
# ./app/helpers/application_helper.rb:6:in `full_title'
# ./app/views/layouts/application.html.erb:4:in `_app_views_layouts_application_html_erb___36363065_93308240'
# ./spec/requests/static_pages_spec.rb:20:in `block (3 levels) in <top (required)>'
2) Static pages Home page should have the base title
Failure/Error: visit '/static_pages/home'
ActionView::Template::Error:
undefined local variable or method `base_title' for #<#<Class:0xb0ea7f8>:0x961e58c>
# ./app/helpers/application_helper.rb:6:in `full_title'
# ./app/views/layouts/application.html.erb:4:in `_app_views_layouts_application_html_erb___36363065_93308240'
# ./spec/requests/static_pages_spec.rb:15:in `block (3 levels) in <top (required)>'
3) Static pages Home page should have the content 'Sample App'
Failure/Error: visit '/static_pages/home'
ActionView::Template::Error:
undefined local variable or method `base_title' for #<#<Class:0xb0ea7f8>:0x9c13244>
# ./app/helpers/application_helper.rb:6:in `full_title'
# ./app/views/layouts/application.html.erb:4:in `_app_views_layouts_application_html_erb___36363065_93308240'
# ./spec/requests/static_pages_spec.rb:10:in `block (3 levels) in <top (required)>'
Finished in 1.23 seconds
3 examples, 3 failures
Failed examples:
rspec ./spec/requests/static_pages_spec.rb:19 # Static pages Home page should not have a custom page title
rspec ./spec/requests/static_pages_spec.rb:14 # Static pages Home page should have the base title
rspec ./spec/requests/static_pages_spec.rb:9 # Static pages Home page should have the content 'Sample App'
Can anyone tell me what's going wrong here?

It feels like you've not posted the exact code for what the tests are running against. I'm not seeing how by what you've posted the code is even getting to full_title in the helper as you only have yeild(:title) in application.html.erb. Nothing is calling full_title.
Looking at Michaels example in the book you need to have full_title(yield(:title)) in the title tags removing what you've got posted there.

Related

Hartl's Rails Tutorial Section 3.5

I've been working through Michael Hartl's Rails tutorial, and for some reason I've gotten stuck on the first exercise in section 3. I've checked and rechecked my code to ensure that it matches his, but I still get this error:
Failures:
1) Static pages Contact page should have the content 'Contact'
Failure/Error: expect(page).to have_content('Contact')
expected #has_content?("Contact") to return true, got false
# ./spec/requests/static_pages_spec.rb:48:in `block (3 levels) in <top (required)>'
2) Static pages Contact page should have the title 'Contact'
Failure/Error: expect(page).to have_title("Ruby on Rails Tutorial Sample App | Contact")
expected #has_title?("Ruby on Rails Tutorial Sample App | Contact") to return true, got false
# ./spec/requests/static_pages_spec.rb:53:in `block (3 levels) in <top (required)>'
Finished in 0.09624 seconds
8 examples, 2 failures
Failed examples:
rspec ./spec/requests/static_pages_spec.rb:46 # Static pages Contact page should have the content 'Contact'
rspec ./spec/requests/static_pages_spec.rb:51 # Static pages Contact page should have the title 'Contact'
Here is my code
static_pages_spec.rb
require 'spec_helper'
describe "Static pages" do
describe "Home page" do
it "should have the content 'Sample App'" do
visit '/static_pages/home'
expect(page).to have_content('Sample App')
end
it "should have the title 'Home'" do
visit '/static_pages/home'
expect(page).to have_title("Ruby on Rails Tutorial Sample App | Home")
end
end
describe "Help page" do
it "should have the content 'Help'" do
visit '/static_pages/help'
expect(page).to have_content('Help')
end
it "should have the title 'Help'" do
visit '/static_pages/help'
expect(page).to have_title("Ruby on Rails Tutorial Sample App | Help")
end
end
describe "About page" do
it "should have the content 'About Us'" do
visit '/static_pages/about'
expect(page).to have_content('About Us')
end
it "should have the title 'About Us'" do
visit '/static_pages/about'
expect(page).to have_title("Ruby on Rails Tutorial Sample App | About Us")
end
end
describe "Contact page" do
it "should have the content 'Contact'" do
visit '/static_pages/about'
expect(page).to have_content('Contact')
end
it "should have the title 'Contact'" do
visit '/static_pages/about'
expect(page).to have_title("Ruby on Rails Tutorial Sample App | Contact")
end
end
end
application.html.erb, which is saved under app/views/layouts
<!DOCTYPE html>
<html>
<head>
<title>Ruby on Rails Tutorial Sample App | <%= yield(:title) %></title>
<%= stylesheet_link_tag "application", media: "all",
"data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
contact.html.erb
<% provide(:title, 'Contact') %>
<h1>Contact</h1>
<p>
Contact Ruby on Rails Tutorial about the sample app at the
contact page.
</p>
routes.rb
SampleApp::Application.routes.draw do
get "static_pages/home"
get "static_pages/help"
get "static_pages/about"
get "static_pages/contact"
end
static_pages_controller.rb
class StaticPagesController < ApplicationController
def home
end
def help
end
def about
end
def contact
end
end
With the limited information you're providing (I know it's hard to diagnose when you're a beginner, it gets easier). It looks like you're telling your test to check your about page and checking for content that only exists in your contact page.

RSpec spec fail and I can't figure it out

I hate to be a help-vampire, but I'm new and I've been looking at this almost two days.
I'm working on Ruby on Rails Tutorial by Michael Hartl. I'm running the test in listing 4.4, which I will include below. The specific test that I'm trying to get to pass checks to see if the page has a selector with the text, "Ruby on Rails Tutorial Sample App". I'll probably find the answer to this problem during the process of asking it on SO, but here we go. Below are, what I believe, the relevant files:
/spec/requests/static_pages_spec.rb
require 'spec_helper'
describe "StaticPages" do
describe "Home page" do
it "should have the h1 'Sample App'" do
visit '/static_pages/home'
page.should have_selector('h1', :text => 'Sample App')
end
it "should have the base title" do
visit '/static_pages/home'
page.should have_selector('title', :text => "Ruby on Rails Tutorial Sample App")
end
it "should not have a custom page title" do
visit '/static_pages/home'
page.should_not have_selector('title', :text => '| Home')
end
end
...[other tests for other pages, let me know if you think they're necessary]
end
app/helpers/application_helper.rb
module ApplicationHelper
# It is a good idea to put controller specific helpers in their
# related helper file (e.g., helpers specifically for the
# StaticPages controller).
# Returns the full title on a per-page basis:
# This helper will be used for each page's title (unless
# otherwise noted).
def full_title(page_title)
base_title = "Ruby on Rails Tutorial Sample App"
if page_title.empty?
base_title
else
"#{base_title} | #{page_title}"
end
end
end
app/views/layouts/application.html.erb
<!DOCTYPE html>
<html>
<head>
<title><%= full_title(yield(:title))%></title>
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
app/views/static_pages/home.html.erb
<% provide :title, "Home" %>
<h1>Sample App</h1>
<p>
This is the home page for a
Ruby on Rails tutorial.
</p>
<p>
It's by Michael Hartl
</p>
I have not configured Sublime to run rspec in the console, yet. When I run the test in Terminal from a bash, I get the following:
.......F.
Failures:
1) StaticPages Home page should have the base title
Failure/Error: page.should have_selector('title', :text => "Ruby on Rails Tutorial Sample App")
expected #has_selector?("title", {:text=>"Ruby on Rails Tutorial Sample App"}) to return true, got false
# ./spec/requests/static_pages_spec.rb:14:in `block (3 levels) in <top (required)>'
Finished in 0.14273 seconds
9 examples, 1 failure
Failed examples:
rspec ./spec/requests/static_pages_spec.rb:12 # StaticPages Home page should have the base title
Randomized with seed #####
When I look at the source code in chrome, I can clearly see a pair of tags with "Ruby on Rails Tutorial Sample App | Home" in between them.
Well, I've gone through it all and I can't figure out what's going on. Hello, SO world! Please help. Thank you.
I would push to github right now, but they're down. I'll update when possible.
change
have_selector('title', :text => "Ruby on Rails Tutorial Sample App")
to
have_title("Ruby on Rails Tutorial Sample App | Home")
It's a change to Capybara and was updated in Michael Hartl's tutorial. To see the update search for "Change have_selector(’title’, …) to have_title(…)" once in the tutorial.

Ruby on Rails embedded ruby generating errors

I am working my way through Ruby on Rails 2nd Ed., Hartl. In section 3.3.3 my tests failed after I removed some static text with some embedded Ruby. The test was passing previously no problem.
rspec
require 'spec_helper'
describe "Home pages" do
describe "Home page" do
it "should have the h1 'Sample App'" do
visit '/static_pages/home'
page.should have_selector('h1', :text => 'Sample App')
end
it "should have the title 'Home'" do
visit '/static_pages/home'
page.should have_selector('title', :text => "Ruby on Rails Tutorial Sample App | Home")
end
end
end
home.html.erb
<% provide (:title, 'Home') %>
<!DOCTYPE html>
<html>
<head>
<title>Ruby on Rails Tutorial Sample App | <%= yield(:title) %></title>
</head>
<body>
<h1>Sample App</h1>
<p>
This is the home page for the
Ruby on Rails Tutorial
sample application.
</p>
</body>
</html>
error from console
C:\Sites\rails_projects\sample_app>bundle exec rspec spec/requests/static_pages_spec.rb
?[31mF?[0m?[31mF?[0m?[32m.?[0m?[32m.?[0m?[32m.?[0m?[32m.?[0m
Failures:
1) Home pages Home page should have the h1 'Sample App'
?[31mFailure/Error:?[0m ?[31mvisit '/static_pages/home'?[0m
?[31mActionView::Template::Error:?[0m
? [31mC:/Sites/rails_projects/sample_app/app/views/static_pages/home.html.erb:1: syntax error,
unexpected ',', expecting ')'?[0m
?[31m...putBuffer.new; provide (:title, ' Home') ?[0m
?[31m... ^?[0m
? [31mC:/Sites/rails_projects/sample_app/app/views/static_pages/home.html.erb:1: syntax error,
unexpected ')', expecting keyword_end?[0m
?[31m....new; provide (:title, ' Home') ?[0m
?[31m... ^?[0m
?[36m # <internal:prelude>:10:in `synchronize'?[0m
?[36m # ./spec/requests/static_pages_spec.rb:7:in `block (3 levels) in <top (required)>'?[0m
2) Home pages Home page should have the title 'Home'
?[31mFailure/Error:?[0m ?[31mvisit '/static_pages/home'?[0m
?[31mActionView::Template::Error:?[0m
? [31mC:/Sites/rails_projects/sample_app/app/views/static_pages/home.html.erb:1: syntax error,
unexpected ',', expecting ')'?[0m
?[31m...putBuffer.new; provide (:title, ' Home') ?[0m
?[31m... ^?[0m
? [31mC:/Sites/rails_projects/sample_app/app/views/static_pages/home.html.erb:1: syntax error,
unexpected ')', expecting keyword_end?[0m
?[31m....new; provide (:title, ' Home') ?[0m
?[31m... ^?[0m
?[36m # <internal:prelude>:10:in `synchronize'?[0m
?[36m # ./spec/requests/static_pages_spec.rb:12:in `block (3 levels) in <top (required)>'?[0m
Finished in 0.51562 seconds
?[31m6 examples, 2 failures?[0m
Failed examples:
?[31mrspec ./spec/requests/static_pages_spec.rb:6?[0m ?[36m# Home pages Home page should have the h1 'Sample App'?[0m
?[31mrspec ./spec/requests/static_pages_spec.rb:11?[0m ?[36m# Home pages Home page should have the title 'Home'?[0m
Any help is appreciated
I think you need to remove the whitespace prior to the parenthesis
provide (:title, 'Home')
should be
provide(:title, 'Home')

RSpec Failure: "Static Pages X should have title X" -- Tried with have_xpath & have_selector

I've been scouring the web & Stack Overflow to tr and figure out how to get these tests working. I'm brand new to Ruby & to Rails, I'm simply following along Hartl's tutorial - copy pasting most of the code to see how it all falls together in the end.
Now, I'm getting stuck in section 3.3 "Slightly Dynamic Pages".
This is the error I'm recieving:
C:\Sites\sample_app>bundle exec rspec --no-color spec/requests/static_pages_spec.rb
F.F.F.
Failures:
1) Static pages About page should have the title 'About Us'
Failure/Error: page.should have_selector('title',
expected css "title" with text "Ruby on Rails Tutorial Sample App | About Us" to return something
# ./spec/requests/static_pages_spec.rb:44:in `block (3 levels) in <top (required)>'
2) Static pages Help page should have the title 'Help'
Failure/Error: page.should have_selector('title',
expected css "title" with text "Ruby on Rails Tutorial Sample App | Help" to return something
# ./spec/requests/static_pages_spec.rb:29:in `block (3 levels) in <top (required)>'
3) Static pages Home page should have the title 'Home'
Failure/Error: page.should have_selector('title',
expected css "title" with text "Ruby on Rails Tutorial Sample App | Home" to return something
# ./spec/requests/static_pages_spec.rb:14:in `block (3 levels) in <top (required)>'
Finished in 3.09 seconds
6 examples, 3 failures
Failed examples:
rspec ./spec/requests/static_pages_spec.rb:42 # Static pages About page should have the title 'About Us'
rspec ./spec/requests/static_pages_spec.rb:27 # Static pages Help page should have the title 'Help'
rspec ./spec/requests/static_pages_spec.rb:12 # Static pages Home page should have the title 'Home'
Randomized with seed 25648
The error is occuring as soon as I swtich from this HTML Structure (in my About/Home/Help.html.erb files):
<% provide(:title, 'Home') %>
<!DOCTYPE html>
<html>
<head>
<title>Ruby on Rails Tutorial Sample App | <%= yield(:title) %></title>
</head>
<body>
<h1>Sample App</h1>
<p>
This is the home page for the
Ruby on Rails Tutorial
sample application.
</p>
</body>
</html>
To this:
<% provide(:title, 'Home') %>
<h1>Sample App</h1>
<p>
This is the home page for the
Ruby on Rails Tutorial
sample application.
</p>
Other related files:
Application.html.erb:
<!DOCTYPE html>
<html>
<head>
<title>Ruby on Rails Tutorial Sample App | <%= yield(:title) %></title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
static_pages_spec.rb
require 'spec_helper'
describe "Static pages" do
describe "Home page" do
it "should have the h1 'Sample App'" do
visit '/static_pages/home'
page.should have_selector('h1', :text => 'Sample App')
end
it "should have the title 'Home'" do
visit '/static_pages/home'
page.should have_selector('title',
:text => "Ruby on Rails Tutorial Sample App | Home")
// The line below is something I tried replacing the page.should have_selector with
// page.should have_xpath("//title", :text => "Home")
end
end
.... (other describe pages, same structure)
end
I might've just gone blind over the course of copying/pasting/reading from Hartl's tutorial, but I am pretty damn sure it looks the same way as he described it.
I've done my best at looking for a solution to this, but alas I haven't been able to figure it out, so here goes!
Cheers!
Edit: Answer to Fiona #1
On this url : "http://localhost:3000/static_pages/home"
The document title is nothing.
The source of the document looks like this:
<h1>Sample App</h1>
<p>
This is the home page for the
Ruby on Rails Tutorial
sample application.
</p>
There is no doctype, head, body or title declaration in the source.
For Rails to pick up the layout file, the application.html.erb file should be in app/views/layouts/application.html.erb.

issue with helper method in rails

I have the following helper method (app/helpers/application_helper.rb):
module ApplicationHelper
#Return a title on a per-page basis
def title
base_title = "Ruby on Rails Tutorial Sample App"
if #title.nil?
base_title
else
"#{base_title} | #{#title}"
end
end
end
and here's the erb ( app/views/layouts/application.html.erb):
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<%= csrf_meta_tag %>
</head>
<body>
<%= yield %>
</body>
</html>
I ran an rspec test to see if this helper method works and it seems that it can't find title.
Here's the error message:
Failures:
1) PagesController GET 'home' should be successful
Failure/Error: get 'home'
ActionView::Template::Error:
undefined local variable or method `title' for #<#<Class:0x991ecb4>:0x991315c>
# ./app/views/layouts/application.html.erb:4:in `_app_views_layouts_application_html_erb__248109341_80250010__979063050'
# ./spec/controllers/pages_controller_spec.rb:8:in `block (3 levels) in <top (required)>'
2) PagesController GET 'home' should have the right title
Failure/Error: get 'home'
ActionView::Template::Error:
undefined local variable or method `title' for #<#<Class:0x991ecb4>:0x9d7d094>
# ./app/views/layouts/application.html.erb:4:in `_app_views_layouts_application_html_erb__248109341_82566280__979063050'
# ./spec/controllers/pages_controller_spec.rb:13:in `block (3 levels) in <top (required)>'
Can anyone tell me what I did wrong?
UPDATE:
I included the helper by doing the following:
describe PagesController do
include ApplicationHelper
render_views
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.should have_selector("title",
:content => "Ruby on Rails Tutorial Sample App | Home")
end
end
//and some more
However I am still getting the same error
In your views, the helpers are not included by default.
You can mock out the helper methods using the template object:
template.should_receive(:title).and_return("Title")
You can then test your helpers separately.
Alternatively you can include your helpers in your view spec by simply doing:
include ApplicationHelper
EDIT
describe PagesController do
include ApplicationHelper
describe "GET 'home'" do
it "should be successful" do
controller.template.should_receive(:title).and_return("Title")
get 'home'
response.should be_success
end
end
end

Resources