[rspec-users] Can't get running a simple rspec test: assigns is always nil

Stephen Eley sfeley at gmail.com
Wed Oct 21 13:59:09 EDT 2009

On Wed, Oct 21, 2009 at 1:07 PM, Alexander Seidl <lists at ruby-forum.com> wrote:
> But why does this filter prevents the index action from being executed?

Because returning 'false' from a filter does exactly that: it prevents
the execution of the controller action.

I'll bet I know what you're thinking -- you're thinking, "But I
override @login_user in my example group's before(:each) code!"

Yes, you did.  But you didn't prevent the filter from executing, and
the first line of your logged_in? method sets the value of @login_user
*again*, even if it was already set.  As you have it here, you'll be
doing that database lookup every single time current_user is called or
an action is called in this controller.

I would suggest changing that first line of the filter from
"@login_user = " to "@login_user ||= " -- which sets the value only if
it was previously false or nil.  Not only will this make your testing
code work, but it'll eliminate any lookup redundancy.

Have Fun,
   Steve Eley (sfeley at gmail.com)
   ESCAPE POD - The Science Fiction Podcast Magazine

More information about the rspec-users mailing list