# How to fix assigns(:user) on controller test

**URL:** <https://www.ruby-forum.com/t/how-to-fix-assigns-user-on-controller-test/114168>\
**Category:** Rails\
**Created:** [September 9, 2007, 1:00am UTC](https://www.ruby-forum.com/t/how-to-fix-assigns-user-on-controller-test/114168 "2007-09-09T01:00:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![OnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/51bf81/32.png) [@OnRails](https://www.ruby-forum.com/u/OnRails)\
**Post date:** [September 9, 2007, 1:00am UTC](https://www.ruby-forum.com/t/how-to-fix-assigns-user-on-controller-test/114168/1 "2007-09-09T01:00:10Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![OnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/51bf81/32.png) [@OnRails](https://www.ruby-forum.com/u/OnRails)\
**Post date:** [September 9, 2007, 4:42am UTC](https://www.ruby-forum.com/t/how-to-fix-assigns-user-on-controller-test/114168/2 "2007-09-09T04:42:21Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![OnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/51bf81/32.png) [@OnRails](https://www.ruby-forum.com/u/OnRails)\
**Post date:** [September 9, 2007, 12:10pm UTC](https://www.ruby-forum.com/t/how-to-fix-assigns-user-on-controller-test/114168/3 "2007-09-09T12:10:48Z")

</div>

OnRails wrote:

> ```
> user.password = params[:password]
> else
> 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 A.

---

<div class="post-metadata">

**Author:** ![OnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/51bf81/32.png) [@OnRails](https://www.ruby-forum.com/u/OnRails)\
**Post date:** [September 9, 2007, 5:57pm UTC](https://www.ruby-forum.com/t/how-to-fix-assigns-user-on-controller-test/114168/4 "2007-09-09T17:57:34Z")

</div>

agree Jacob A…

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

2007/9/9, Jacob A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![OnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/51bf81/32.png) [@OnRails](https://www.ruby-forum.com/u/OnRails)\
**Post date:** [September 9, 2007, 11:03pm UTC](https://www.ruby-forum.com/t/how-to-fix-assigns-user-on-controller-test/114168/5 "2007-09-09T23:03:14Z")

</div>

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