[rspec-users] running: 'rake spec' mixes ApplicationHelper into my ActiveRecord model instances

Stephen Bannasch stephen.bannasch at deanbrook.org
Fri Apr 23 01:32:20 EDT 2010


solved ...

Running the ApplicationHelper spec tests mixed the ApplicationHelper methods into Object.

Actually I don't remember making the helper being tested or the spec ;-) Might have been generated by restful authentication ...

Here's what's in spec/helpers/application_helper_spec.rb (which is what is causing the problem):

   require File.dirname(__FILE__) + '/../spec_helper'
   include ApplicationHelper
   include AuthenticatedTestHelper

   describe ApplicationHelper do

     describe "title" do
       it "should set @page_title" do
         title('hello').should be_nil
         @page_title.should eql('hello')
       end
    
       it "should output container if set" do
         title('hello', :h2).should have_tag('h2', 'hello')
       end
     end

   end

Evidently self is Object when that file is loaded and mixes the methods in ApplicationHelper into Object -- not useful -- but 
I'll bet I'm still doing something wrong ...

Here's what I replaced it with which works fine:

   require 'spec_helper'

   class ViewWithApplicationHelper < ActionView::Base
      include ApplicationHelper
      # When actually running in a view the controller's instance variables are
      # mixed into the object that is self when rendering, but instances
      # created from a class that just inherits from ActionView::Base don't have
      # those instance variables (like @page_title) mixed in. So I add it by hand.
      attr_accessor :page_title  
   end

   describe ViewWithApplicationHelper do

     before(:each) do
       @view = ViewWithApplicationHelper.new
     end

     describe "title" do
       it "should set @page_title" do
         @view.title('hello').should be_nil
         @view.page_title.should eql('hello')
       end
    
       it "should output container if set" do
         @view.title('hello', :h2).should have_tag('h2', 'hello')
       end
     end

   end


http://gist.github.com/375634 updated with the description of using ruby-debug to find this problem.



More information about the rspec-users mailing list