Hi everyon,
Thank you for your any help!
I did the controller test, but there is a problem where run the test.
On controller.rb:
def change_password
user = current_user
if User.authenticate(user.login, params[:old_password])
if (params[:password] == params[:password_confirmation])
user.password_confirmation = params[:password_confirmation]
user.password = params[:password]
if user.save
result = _("Password changed!")
reset_current_user(user.id)
else
("Could not save change, please try again.")
end
else
result = _("Your passwords do not match.")
@old_password = params[:old_password]
end
else
result = _("Your old password is incorrect.")
end
end
the controller_test.rb:
def test_should_allow_password_change
post :change_password, { :old_password => ''123'',
:password =>
''newpassword'', :password_confirmation =>
''newpassword''},
{:user_id=>users(:user1).id}
assert_equal ''newpassword'', assigns(:user).password
end
when I run this test, There always said,
NoMetodError: You have a ni object when you didn''t expect it:
the error occurred while evaluating nil.password.
why did happen that? I looked around but I can''t find the way to solve
it.
Please help1
Thanks!
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
OnRails wrote:> Hi everyon, > Thank you for your any help! > I did the controller test, but there is a problem where run the test. > On controller.rb: > > def change_password > user = current_user > if User.authenticate(user.login, params[:old_password]) > if (params[:password] == params[:password_confirmation]) > user.password_confirmation = params[:password_confirmation] > user.password = params[:password] > if user.save > result = _("Password changed!") > reset_current_user(user.id) > else > ("Could not save change, please try again.") > end > else > result = _("Your passwords do not match.") > @old_password = params[:old_password] > end > else > result = _("Your old password is incorrect.") > end > end > > the controller_test.rb: > > def test_should_allow_password_change > post :change_password, { :old_password => ''123'', :password => > ''newpassword'', :password_confirmation => ''newpassword''}, > {:user_id=>users(:user1).id} > assert_equal ''newpassword'', assigns(:user).password > end > > when I run this test, There always said, > NoMetodError: You have a ni object when you didn''t expect it: > the error occurred while evaluating nil.password. > > why did happen that? I looked around but I can''t find the way to solve > it.Assigns only works for instance variables. You will have to use: @user = current_user if you want access to the variable in the test. Though I believe you shouldn''t poke around in the internals of your controller action from the test, rather test the updated user from the database, like: assert_equal ''newpassword'', users(:user1).reload.password -- Cheers, - Jacob Atzen --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
agree Jacob Atzen. but , it''s looks like ,functional is not very usefull. 2007/9/9, Jacob Atzen <jacob-4U2y0bnePT5NzRJJ8cAMrg@public.gmane.org>:> > OnRails wrote: > > Hi everyon, > > Thank you for your any help! > > I did the controller test, but there is a problem where run the test. > > On controller.rb: > > > > def change_password > > user = current_user > > if User.authenticate(user.login, params[:old_password]) > > if (params[:password] == params[:password_confirmation]) > > user.password_confirmation = params[:password_confirmation] > > user.password = params[:password] > > if user.save > > result = _("Password changed!") > > reset_current_user(user.id) > > else > > ("Could not save change, please try again.") > > end > > else > > result = _("Your passwords do not match.") > > @old_password = params[:old_password] > > end > > else > > result = _("Your old password is incorrect.") > > end > > end > > > > the controller_test.rb: > > > > def test_should_allow_password_change > > post :change_password, { :old_password => ''123'', :password => > > ''newpassword'', :password_confirmation => ''newpassword''}, > > {:user_id=>users(:user1).id} > > assert_equal ''newpassword'', assigns(:user).password > > end > > > > when I run this test, There always said, > > NoMetodError: You have a ni object when you didn''t expect it: > > the error occurred while evaluating nil.password. > > > > why did happen that? I looked around but I can''t find the way to solve > > it. > > Assigns only works for instance variables. You will have to use: > > @user = current_user > > if you want access to the variable in the test. Though I believe you > shouldn''t poke around in the internals of your controller action from > the test, rather test the updated user from the database, like: > > assert_equal ''newpassword'', users(:user1).reload.password > > -- > Cheers, > - Jacob Atzen > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thank you very much, I think so. I''''ll try it. On Sep 9, 3:10 am, Jacob Atzen <ja...-4U2y0bnePT5NzRJJ8cAMrg@public.gmane.org> wrote:> OnRails wrote: > > Hi everyon, > > Thank you for your any help! > > I did the controller test, but there is a problem where run the test. > > On controller.rb: > > > def change_password > > user = current_user > > if User.authenticate(user.login, params[:old_password]) > > if (params[:password] == params[:password_confirmation]) > > user.password_confirmation = params[:password_confirmation] > > user.password = params[:password] > > if user.save > > result = _("Password changed!") > > reset_current_user(user.id) > > else > > ("Could not save change, please try again.") > > end > > else > > result = _("Your passwords do not match.") > > @old_password = params[:old_password] > > end > > else > > result = _("Your old password is incorrect.") > > end > > end > > > the controller_test.rb: > > > def test_should_allow_password_change > > post :change_password, { :old_password => ''123'', :password => > > ''newpassword'', :password_confirmation => ''newpassword''}, > > {:user_id=>users(:user1).id} > > assert_equal ''newpassword'', assigns(:user).password > > end > > > when I run this test, There always said, > > NoMetodError: You have a ni object when you didn''t expect it: > > the error occurred while evaluating nil.password. > > > why did happen that? I looked around but I can''t find the way to solve > > it. > > Assigns only works for instance variables. You will have to use: > > @user = current_user > > if you want access to the variable in the test. Though I believe you > shouldn''t poke around in the internals of your controller action from > the test, rather test the updated user from the database, like: > > assert_equal ''newpassword'', users(:user1).reload.password > > -- > Cheers, > - Jacob Atzen- Hide quoted text - > > - Show quoted text ---~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---