2

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

adit
  • 32,574
  • 72
  • 229
  • 373

1 Answers1

2

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
Gazler
  • 83,029
  • 18
  • 279
  • 245
  • where do I put the include ApplicationHelper at? – adit Aug 23 '11 at 18:20
  • If I remember correctly it needs to be inside a describe block. **Edit** Just found this link http://www.multunus.com/2011/06/rspec-issue-with-include-helper-in-spec/ – Gazler Aug 23 '11 at 18:21
  • Apologies, I assumed you were testing a view when you mentioned the helper methods. Did you try the stubbing method? – Gazler Aug 23 '11 at 18:39
  • I am not that much familiar with rspec so I don't know where to put that line, it says to put in the template object, but where is it? – adit Aug 23 '11 at 18:42
  • See my edits, and tell me if that works. I also recommend checking out http://kerryb.github.com/iprug-rspec-presentation/ – Gazler Aug 23 '11 at 18:45
  • If you see my UPDATE/EDIT, I am actually applying the method that you mentioned on that link, includeing the module inside a describe block and it didn't work – adit Aug 23 '11 at 19:01
  • @adit let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/2783/discussion-between-gazler-and-adit) – Gazler Aug 23 '11 at 19:02