Heinz Strunk
2010-Aug-07 15:05 UTC
Protected ApplicationController methods not called in Tests?
Hey, I''m currently writing some tests and getting a strange error. My ApplicationController has following protected method: before_filter :load_user_vars def load_user_vars @current_user = current_user [other protected method] @current_character = @current_user.character ... end When I now run a test and the @current_character is being used in a controller that is being tested I get a "RuntimeError: Called id for nil" Why is that protected method not being called since it''s a before_filter method? And how can I make my test to run this method and declare these variables? Thanks! -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Chris Mear
2010-Aug-07 15:23 UTC
Re: Protected ApplicationController methods not called in Tests?
On 7 August 2010 16:05, Heinz Strunk <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I''m currently writing some tests and getting a strange error. My > ApplicationController has following protected method: > before_filter :load_user_vars > > def load_user_vars > @current_user = current_user [other protected method] > @current_character = @current_user.character > ... > end > > When I now run a test and the @current_character is being used in a > controller that is being tested I get a "RuntimeError: Called id for > nil" > > Why is that protected method not being called since it''s a before_filter > method? And how can I make my test to run this method and declare these > variables?How do you conclude that load_user_vars isn''t being run? Perhaps it is being run, but @current_user.character is just returning nil (which seems more likely). It''s hard to be sure without seeing some more code. Could you Gist [1] your ApplicationController, and the code where the RuntimeError is being raised? Chris [1] http://gist.github.com/ -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2010-Aug-07 15:24 UTC
Re: Protected ApplicationController methods not called in Tests?
On 7 August 2010 16:05, Heinz Strunk <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hey, > > I''m currently writing some tests and getting a strange error. My > ApplicationController has following protected method: > before_filter :load_user_vars > > def load_user_vars > @current_user = current_user [other protected method] > @current_character = @current_user.character > ... > end > > When I now run a test and the @current_character is being used in a > controller that is being tested I get a "RuntimeError: Called id for > nil" > > Why is that protected method not being called since it''s a before_filter > method? And how can I make my test to run this method and declare these > variables?It certainly should be called from tests. Are you sure it is not being called? Try putting a line debugger at the start of the filter and see if it breaks there. Otherwise how are you invoking the controller method that should call the filter? Have you looked in test.log to see if there is anything of note there? If still no joy show us the controller action code and the error trace and which line it is failing on. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Heinz Strunk
2010-Aug-07 16:00 UTC
Re: Protected ApplicationController methods not called in Tests?
Sorry guys, It took me only like ten times going through the code to find a typo that caused the problem. It''s working now, thanks for the quick help! -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.