Alexander Seidl
2009-Oct-20 12:23 UTC
[rspec-users] Can''t get running a simple rspec test: assigns is always nil
http://pastie.org/pastes/661816 hi, please have a look at my code. Perhaps anybody knows a solution for that problem. Attachments: http://www.ruby-forum.com/attachment/4165/pastie-661816.txt -- Posted via http://www.ruby-forum.com/.
Pat Maddox
2009-Oct-21 08:25 UTC
[rspec-users] Can''t get running a simple rspec test: assigns is always nil
Is this all of the code, or did you remove some stuff? I copied the code in a new rails app and the test passed. What versions of rspec & rspec-rails are you running? Pat On Tue, Oct 20, 2009 at 5:23 AM, Alexander Seidl <lists at ruby-forum.com> wrote:> http://pastie.org/pastes/661816 > > hi, > please have a look at my code. Perhaps anybody knows a solution for that > problem. > > Attachments: > http://www.ruby-forum.com/attachment/4165/pastie-661816.txt > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Alexander Seidl
2009-Oct-21 10:58 UTC
[rspec-users] Can''t get running a simple rspec test: assigns is always nil
hi pat, thats all of the code that will be executed in categories_controller_spec.rb. the rest is commented out. i use the newest version of both gems? could that be a bug. which versions of rpsec and rspec-rails do you use? alexanders at alexanders-desktop:$ gem list -l rspec *** LOCAL GEMS *** rspec (1.2.9, 1.2.8) rspec-rails (1.2.9, 1.2.7.1) cheers, Alexander -- Posted via http://www.ruby-forum.com/.
Alexander Seidl
2009-Oct-21 11:03 UTC
[rspec-users] Can''t get running a simple rspec test: assigns is always nil
im repeating this message here in the mailing list with an email. i already answered the message at http://www.ruby-forum.com, but it seems that delivering messages via http://www.ruby-forum.com lasts way too long, so i write an email to the mailing list. hi pat, thats all of the code that will be executed in categories_controller_spec.rb. the rest is commented out. i use the newest version of both gems? could that be a bug. which versions of rpsec and rspec-rails do you use? alexanders at alexanders-desktop:$ gem list -l rspec *** LOCAL GEMS *** rspec (1.2.9, 1.2.8) rspec-rails (1.2.9, 1.2.7.1) cheers, Alexander Pat Maddox schrieb:> Is this all of the code, or did you remove some stuff? I copied the > code in a new rails app and the test passed. What versions of rspec & > rspec-rails are you running? > > Pat > > On Tue, Oct 20, 2009 at 5:23 AM, Alexander Seidl <lists at ruby-forum.com> wrote: > >> http://pastie.org/pastes/661816 >> >> hi, >> please have a look at my code. Perhaps anybody knows a solution for that >> problem. >> >> Attachments: >> http://www.ruby-forum.com/attachment/4165/pastie-661816.txt >> >> -- >> Posted via http://www.ruby-forum.com/. >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> >> > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > >
Pat Maddox
2009-Oct-21 15:30 UTC
[rspec-users] Can''t get running a simple rspec test: assigns is always nil
On Wed, Oct 21, 2009 at 3:58 AM, Alexander Seidl <lists at ruby-forum.com> wrote:> hi pat, > thats all of the code that will be executed in > categories_controller_spec.rb. the rest is commented out.Looks like this may be the same issue as the other thread. You didn''t show all the code, because there''s an unknown UserController :) I''m going to stop responding in this thread, and focus on the other one, because I suspect that both problems you''re experiencing have the same root cause. Pat
Alexander Seidl
2009-Oct-21 17:07 UTC
[rspec-users] Can''t get running a simple rspec test: assigns is always nil
Pat Maddox wrote:> On Wed, Oct 21, 2009 at 3:58 AM, Alexander Seidl <lists at ruby-forum.com> > wrote: >> hi pat, >> thats all of the code that will be executed in >> categories_controller_spec.rb. the rest is commented out. > > Looks like this may be the same issue as the other thread. You didn''t > show all the code, because there''s an unknown UserController :) I''m > going to stop responding in this thread, and focus on the other one, > because I suspect that both problems you''re experiencing have the same > root cause. > > PatYou are completely right! There IS a before_filter in UserController, which executes this method before every action: http://pastie.org/663761 But why does this filter prevents the index action from being executed? Alexander -- Posted via http://www.ruby-forum.com/.
Stephen Eley
2009-Oct-21 17:59 UTC
[rspec-users] Can''t get running a simple rspec test: assigns is always nil
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 http://www.escapepod.org
Alexander Seidl
2009-Oct-26 08:43 UTC
[rspec-users] Can''t get running a simple rspec test: assigns is always nil
Hi Stephan, thank you very much for this comprehensive answer. And thanks to all others participating at this thread. This will help me on my further attempts to write tests with rspec. Cheers, Alexander Stephen Eley wrote:> 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 > http://www.escapepod.org-- Posted via http://www.ruby-forum.com/.