Andre Schütz
2011-Mar-30 19:10 UTC
Test errors in fuctional test after adding before_filter :login_required to controller
Hello, I added the "before_filter" to my controllers to require a login of the user. Here''s an example of my Unit Controller with the added before_filter: IN THE ATTACHED FILE When executing the tests with rake test, I get different error messages. To show you my errors, I only executed the unit controller test with the following line: ruby -Itest test/functional/units_controller_test.rb I get the folowing errors: IN THE ATTACHED FILE If I delete the "before_filter :login_required" line from the unit controller, the test will be executed without any errors. I have a controller_authentication.rb file within my /lib directory. The content of the file: IN THE ATTACHED FILE Can I use one of the methods to get access for the tests? I tried to find a solution for that problem, but I could not find any during the last two days. Please give me advice to solve that problem. Thanks in advance, Andre Attachments: http://www.ruby-forum.com/attachment/6092/controller_and_error.txt -- 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.
Phil Crissman
2011-Mar-30 19:33 UTC
Re: Test errors in fuctional test after adding before_filter :login_required to controller
Phil 2011/3/30 Andre Schütz <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>> Hello, > > I added the "before_filter" to my controllers to require a login of the > user. > > Here''s an example of my Unit Controller with the added before_filter: > > IN THE ATTACHED FILE > > When executing the tests with rake test, I get different error messages. > > To show you my errors, I only executed the unit controller test with the > following line: > ruby -Itest test/functional/units_controller_test.rb > > I get the folowing errors: > > IN THE ATTACHED FILE > > If I delete the "before_filter :login_required" line from the unit > controller, the test will be executed without any errors. > > I have a controller_authentication.rb file within my /lib directory. The > content of the file: > > IN THE ATTACHED FILE > > Can I use one of the methods to get access for the tests? > > I tried to find a solution for that problem, but I could not find any > during the last two days. > Please give me advice to solve that problem. >You''ll need to create a user and make sure they are logged in, within any of your tests that require the user to be logged in. How you do this depends on you''re authenticating your users...> > Thanks in advance, > Andre > > Attachments: > http://www.ruby-forum.com/attachment/6092/controller_and_error.txt > > > -- > 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. > >-- 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.
Franz Strebel
2011-Mar-30 19:40 UTC
Re: Test errors in fuctional test after adding before_filter :login_required to controller
In your tests, you''ll need to log in a user otherwise the before_filter in your controller redirects to the login page. I suggest you change a portion of your authentication code, in particular def logged_in? # current_user session[:user_id] end So instead of calling current_user, which hits the database to fetch the User by the way, you can just return session[:user_id]. This is good enough to figure out if a user is logged in as it will return nil if the key :user_id is not set, and nil is as good as false in ruby. With that change, you can then set a session variable in your functional test, eg class UnitsControllerTest < ActionController::TestCase def test_index get :index, { }, { ''user_id'' => 1 } assert_template ''index'' end The first hash after the action is for parameters, and the second one is for session variables. Ideally though, you should be testing different scenarios to see what the behavior will be when a user is logged in or not. I highly recommend that you look at shoulda and flexmock for testing. Shoulda allows you to easily nest contexts for your tests while flexmock allows you to set up mocks to avoid hitting the database or setting up session variables as done above. Instead, you can have something like this in your test_helper.rb def be_logged_in flexmock(@controller).should_receive(:logged_in?).and_return(true) end def be_logged_out flexmock(@controller).should_receive(:logged_in?).and_return(false) end Then in your shoulda setup blocks, you just call one of those two helpers depending on the context you are testing. Have fun, Franz -- 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.
Andre Schütz
2011-Mar-31 18:53 UTC
Re: Test errors in fuctional test after adding before_filter :login_required to controller
I found a solution that uses fixtures and executes the tests without any errors. I wrote a short article at: http://www.faustas.de/rails-test-error-beforefilter-loginrequired-controller -- 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.
Reasonably Related Threads
- before_filter order of execution
- before_filter always running in test, not in development or production mode.
- Where can I get "authenticate_with_http_basic"?
- auth_generator 2.0.1 undefined method `login_required' for ... ArticlesController
- Send parameter along with method in before_filter