I''m trying to run some functional tests for actions that are behind authentication (via act as authenticated plugin), but am having problems getting "current_user" set for the controller. Here''s the test code: def test_create num_notes = Note.count post :create, :note => {:title => "Test Note", :msg => "Test Message" } assert_response :redirect assert_redirected_to :action => ''list'' assert_equal num_notes + 1, Note.count end Here''s the contoller code that it is failing: @note.user_id = current_user.id I''ve added this include to the first line of the test class: include AuthenticatedTestHelper In the setup function I''ve added: login_as :jeremy There is a user in the fixure with the login of "jeremy". So what am I doing wrong? --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I found that the default "authenticated_test_helper.rb" file was throwing an error about the "users" method being undefined with the following function: def login_as(user) @request.session[:user] = user ? users(user).id : nil end So I changed it to this: def login_as(user) @request.session[:user] = user ? User.find_by_login(user) : nil end This seemed to fix the problem. Is this an OK modification to make? Why didn''t the code that it shipped with work? On Sep 23, 12:38 am, Mozmonkey <JeremyMail...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m trying to run some functional tests for actions that are behind > authentication (via act as authenticated plugin), but am having > problems getting "current_user" set for the controller. > > Here''s the test code: > > def test_create > num_notes = Note.count > > post :create, :note => {:title => "Test Note", :msg => "Test > Message" } > assert_response :redirect > assert_redirected_to :action => ''list'' > > assert_equal num_notes + 1, Note.count > end > > Here''s the contoller code that it is failing: > > @note.user_id = current_user.id > > I''ve added this include to the first line of the test class: > > include AuthenticatedTestHelper > > In the setup function I''ve added: > > login_as :jeremy > > There is a user in the fixure with the login of "jeremy". So what am > I doing wrong?--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Mozmonkey wrote:> I found that the default "authenticated_test_helper.rb" file was > throwing an error about the "users" method being undefined with the > following function:And you are using the out-of-the-box fixtures system, with fixtures :users above every call to login_as?> def login_as(user) > @request.session[:user] = user ? users(user).id : nil > end > > So I changed it to this: > > def login_as(user) > @request.session[:user] = user ? User.find_by_login(user) : nil > end > > This seemed to fix the problem. Is this an OK modification to make?sessions should only contain numbers (for various definitions of "should"), so you are missing an .id> Why didn''t the code that it shipped with work?Works for me. How sure are you that this assertion would pass before your login_as? assert_not_nil users(:jeremy) Then, I suspect something reconstitutes current_user at page hit time, via User.find_by_id(session[:user]) Storing entire objects in your session table is bad JuJu. -- Phlip http://www.oreilly.com/catalog/9780596510657/ "Test Driven Ajax (on Rails)" assert_xpath, assert_javascript, & assert_ajax --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---