[rspec-users] how to stub the :current_user and any methods associated with it

David Chelimsky dchelimsky at gmail.com
Wed Nov 5 09:14:48 EST 2008

On Wed, Nov 5, 2008 at 8:05 AM, Fernando Perez <lists at ruby-forum.com> wrote:
> I'm also having problems stubbing current_user.
> I cannot put
> controller.stub!(:current_user).and_return(@current_user)
>  in a before(:all) block. The stubbing doesn't happen.

Stubs and mocks get cleared out after each example. before(:all) only
runs once, so after the first example in a group runs, the stub gets
cleared out and does not get recreated.

before(:each) runs before each example, setting the stub each time.

So use before(:each) instead of before(:all).

Make sense?

There's a little more info about this on
http://rspec.info/documentation/before_and_after.html (near the


> However if I put it in the it "should..." block, then it works.
> Why is that?
> --
> 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