[mocha-developer] Fwd: Content filtered message notification

James Mead jamesmead44 at gmail.com
Mon Oct 15 08:02:25 EDT 2007


I'm not sure why mailman bounced your message (perhaps because you are not
subscribed to the mailing list), but I'm glad you have solved your problem.

Cheers, James.

---------- Forwarded message ----------
From: mailman-bounces at rubyforge.org <mailman-bounces at rubyforge.org>
Date: 14 Oct 2007 17:21
Subject: Content filtered message notification
To: mocha-developer-owner at rubyforge.org

The attached message matched the mocha-developer mailing list's
content filtering rules and was prevented from being forwarded on to
the list membership.  You are receiving the only remaining copy of the
discarded message.



---------- Forwarded message ----------
From: Christophe <anaema_ml at yahoo.fr>
To: mocha-developer at rubyforge.org
Date: Sun, 14 Oct 2007 18:15:09 +0200
Subject: Re: [mocha-developer] Stubbing a module method
 le 03/10/2007 10:40, James Mead nous a dit:

On 27/09/2007, Christophe <anaema_ml at yahoo.fr> <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
http://pastie.caboo.se/103234.

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

def test_closed_site
  @controller.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

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?

 Thank you for your answer James (I missed it in the mailing list, so I only
reply now).
You're right, I should have no need to stub this method within an
integration test ! But I had few problems with the login process and
(because I'm lazy) I tought it would be easier to stub the login_required
method.
Yes it's make more sense to stub the controller's method, but unfortunately
under an integration test you get a controller instance AFTER the first
request (the @controller does not exist as in functionnal tests), so
stubbing can't work here.

But today, I've rewritten the integration test and I no more need to stubs
the login method, I followed the right path ;-)

Regards

Christophe Gimenez



-- 
James.
http://blog.floehopper.org
http://tumble.floehopper.org


More information about the mocha-developer mailing list