[rspec-users] Problems mocking controller helpers with the last version.

David Chelimsky dchelimsky at gmail.com
Mon Sep 29 07:54:07 EDT 2008

On Mon, Sep 29, 2008 at 6:35 AM, Juanma Cervera <lists at ruby-forum.com> wrote:
> Thanks Craig, but I think I don't understand completely.
> "current_user" is a helper method provided by the plugin
> restful_authentication
> to access the @current_user variable as I understand.
> It is defined in lib/authenticated_system.rb.
> We use this helper to access the current logged-in user in the system
> from any view.
> Do you think that I have to use the variable directly?

This is precisely why it's better to stub methods than try to set up
internal state (instance variables).

The assigns hash represents instance variables set IN THE VIEW by the
controller action. While it is true that the current_user method from
restful auth uses @current_user, it does so IN THE CONTROLLER, not in
the view. So when the view calls current_user, that call is forwarded
to the controller, which is where the @current_user instance variable

What you can do that will work in this case is to stub the
current_user method directly on the view:


Now the call won't get delegated to the controller, and you don't have
to set up real state in the controller to get a simulated result in
the view. Much less brittle for your isolated examples.

As always, isolated examples don't tell you when the app works as
expected. They tell you when the individual components work as
expected. You still need higher level coverage from a tool like
cucumber or rails integration tests.


> Juanma
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

More information about the rspec-users mailing list