Search code examples
ruby-on-rails-3rspecspork

Why is Rspec Test Failing Here?


I am getting three of the same types of errors when I run Rspec.

My application_controller.rb is this:

<!DOCTYPE>
<html>
        <head>
            <title><%= @title %></title>
            <%= csrf_meta_tag %>
            </head>
        <body>
            <%= yield %>
        </body>
</html>

My pages_controller_spec.rb is this:

require 'spec_helper'

describe PagesController do
  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 | Home")
    end

    it "should have a non-blank body" do
      get 'home'
      response.body.should_not =~ /<body>\s*<\/body>/
    end
  end

  describe "GET 'contact'" do
    it "should be successful" do
      get 'contact'
      response.should be_success
    end

    it "should have the right title" do
      get 'contact'
      response.should have_selector("title",
                        :content => "Ruby on Rails Tutorial Sample | Contact")
    end
  end

  describe "GET 'about'" do
    it "should be successful" do
      get 'about'
      response.should be_success
    end

    it "should have the right title" do
      get 'about'
      response.should have_selector("title",
                        :content => "Ruby on Rails Tutorial Sample | About")
    end
  end
end

My error read-out is this:

 7 examples, 3 failures

Failed examples:

    rspec ./spec/controllers/pages_controller_spec.rb:12 # PagesController GET 'home' should have the right title
rspec ./spec/controllers/pages_controller_spec.rb:30 # PagesController GET 'contact' should have the right title
rspec ./spec/controllers/pages_controller_spec.rb:43 # PagesController GET 'about' should have the right title

My application.html.erb

<!DOCTYPE>
<html>
        <head>
            <title><%= @title %></title>
            <%= csrf_meta_tag %>
            </head>
        <body>
            <%= yield %>
        </body>
</html>

This is my helper file:

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 is my pages controller file:

class PagesController < ApplicationController

      def home
        @title = "Home"
      end

      def contact
        @title = "Contact"
      end

      def about
        @title = "About"
      end
    end

I'm not clear as to why this is failing.


Solution

  • Change your application.html.erb to read title rather than @title as you are using the helper method rather than setting the title from the controller.

    <!DOCTYPE>
    <html>
        <head>
            <title><%= title %></title>
            <%= csrf_meta_tag %>
            </head>
        <body>
            <%= yield %>
        </body>
    </html>