Running: 'rake spec' mixes ApplicationHelper into my ActiveRecord model instances

When I run the a controllers spec test on it’s own I get just what I
expect.

But when I run the same tests with: rake spec I get some strange errors
because ApplicationHelper ends up getting mixed into my
ActiveRecord model instances.

Is this normal? It seems nutty to me – maybe it’s a problem just in my
codebase.

This is a rails 2.3.4 project using these versions of rspec and
rspec-rails:

$ gem list rspec

*** LOCAL GEMS ***

rspec (1.3.0)
rspec-rails (1.3.2)

This gist shows some details of the issue: gist:375634 · GitHub

The code is in the emb-test branch of http://github.com/stepheneb/rigse

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 :wink:
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

gist:375634 · GitHub updated with the description of using
ruby-debug to find this problem.