[rspec-users] Controller spec, integrate views, mocking helper method

LesNightingill codehacker at comcast.net
Mon Aug 2 19:22:56 EDT 2010


I need to use integrate views to test some particular features. In
ApplicationHelper I have:

module ApplicationHelper
     def current_user_permitted?
           # some logic that returns true/false
     end
end

I'm testing MyController using Rspec/flexmock...

describe MyController do
      integrate_views
      before(:each) do
          # how can I mock the 'current_user_permitted?' method here ?
          get :index
      end

     it "should display blah blah" do
          response.should have_tag('div#foo') do
               with_tag('div#bar',{:html => (/blah blah/)} )
          end
     end
end

I've found that I can make the test pass by mocking the
current_user_permitted? like this (BOTH mocks required in order to
pass!):
 
flexmock(ActionView::Base).new_instances.should_receive("current_user_permitted?").and_return(true)
flexmock(controller, 'current_user_permitted?' => true)

but this (2 mocks) just doesn't seem right, what is the right way?

thanks in advance for any insight you can offer

Les


More information about the rspec-users mailing list