0
votes

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 <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
    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?

1
Try moving the let to be after the second describe not the first.Michael Durrant
I've moved let, but I get the same error. Even when I commented this line I get the error.Gvyntyk

1 Answers

0
votes

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.