[mocha-developer] Stubbing a module method

James Mead jamesmead44 at gmail.com
Wed Oct 3 04:40:06 EDT 2007

On 27/09/2007, Christophe <anaema_ml at yahoo.fr> wrote:
> Hi, I'm trying to stub the login_required() method of the
> acts_as_authenticated plugin in an RoR integration test.
> This method is defined in the AuthenticatedSystem module, in the
> lib/authenticated_system.rb
> What happen is that the stub have no effect and the real method is still
> executed.
> Here is the code. Any idea ?
> Thank you
> require "#{File.dirname(__FILE__)}/../test_helper"
> class CheckStopsiteTest < ActionController::IntegrationTest
>   fixtures :users
>   def test_closed_site
>     AuthenticatedSystem.stubs(:login_required).returns(true)
>     get '/admin/maintenance/stop_site'
>     assert_response :success
>     get '/fr/site'
>     assert_redirected_to :controller => 'site', :action => 'closed_site'
>   end
> end

Sorry not to reply sooner - I've been away on holiday.

It's a while since I looked at acts_as_authenticated, but I suspect the
problem is that the login_required method is a module *instance* method and
not a module method. This means you need to stub the method on the instance
into which it has been mixed - probably the controller in this case. The
difference between stubbing a module method and a module instance method is
probably best explained by a couple of quick examples - see

In your test, I suspect you need to do something like...

def test_closed_site
  get '/admin/maintenance/stop_site'
  assert_response :success
  get '/fr/site'
  assert_redirected_to :controller => 'site', :action => 'closed_site'

I hope that solves your problem. Let me know if not.

One last thought - are you sure it makes sense to stub within an
"integration" test?

More information about the mocha-developer mailing list