how to fix assigns(:user) on controller test

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!

This problem is nobody kenow how to solved? I have try to use: assert_equal 'newpassword', assigns('user').password assert_equal 'newpassword', assigns[:user].password assert_equal 'newpassword', assigns['user'].password

but there is same thing. I don't know why? please help!

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

agree Jacob Atzen.

but , it's looks like ,functional is not very usefull.

Thank you very much, I think so. I''ll try it.