[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

how to fix assigns(:user) on controller test

OnRails Ruby

9/9/2007 2:48:00 AM

Hi everyon,
Thank you for your help!
I did the controller test, but there is a problem when run the test.

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.

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 problem. I don't know why?
Please help1
Thanks!
--
Posted via http://www.ruby-....

5 Answers

James Hunt

9/9/2007 7:25:00 AM

0

On 9/8/07, OnRails Ruby <zlai@sina.com> claimed:
> Hi everyon,
> Thank you for your help!
> I did the controller test, but there is a problem when run the test.
>
> 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.
>
> I have try to use:
> assert_equal 'newpassword', assigns('user').password
> assert_equal 'newpassword', assigns[:user].password
> assert_equal 'newpassword', assigns['user'].password
>

Are you using fixtures for your tests? (I assume you are, from the
assigns() usage).

Can you post the fixtures?

> Thanks!
You're welcome

--
James

OnRails Ruby

9/9/2007 8:57:00 PM

0

James Hunt wrote:
> On 9/8/07, OnRails Ruby <zlai@sina.com> claimed:
>>
>> 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.
>>
>> I have try to use:
>> assert_equal 'newpassword', assigns('user').password
>> assert_equal 'newpassword', assigns[:user].password
>> assert_equal 'newpassword', assigns['user'].password
>>
>
> Are you using fixtures for your tests? (I assume you are, from the
> assigns() usage).
>
> Can you post the fixtures?
>
>> Thanks!
> You're welcome

the fixtures file is:
user1:
id: 1
login: testuser1
user_name:asb
password: 123
created_at: 2006-04-12 14:30:25
user2:
id: 2
login: testuser2
user_name:asb123
password: 123
created_at: 2006-04-12 14:30:25



--
Posted via http://www.ruby-....

James Hunt

9/9/2007 9:02:00 PM

0

On 9/9/07, OnRails Ruby <zlai@sina.com> wrote:
> James Hunt wrote:
> > Are you using fixtures for your tests? (I assume you are, from the
> > assigns() usage).
> >
> > Can you post the fixtures?
> >
> the fixtures file is:
> user1:
> id: 1
> login: testuser1
> user_name:asb
> password: 123
> created_at: 2006-04-12 14:30:25
> user2:
> id: 2
> login: testuser2
> user_name:asb123
> password: 123
> created_at: 2006-04-12 14:30:25

Therein lies your problem. You don't have a data item in your
fixture'd database called 'user', only 'user1' and 'user2'

Try
assert_equal 'newpassword', assigns(:user1).password

--
James

Rob Biedenharn

9/9/2007 9:06:00 PM

0

On Sep 9, 2007, at 4:56 PM, OnRails Ruby wrote:
> James Hunt wrote:
>> On 9/8/07, OnRails Ruby <zlai@sina.com> claimed:
>>>
>>> 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.
>>>
>>> I have try to use:
>>> assert_equal 'newpassword', assigns('user').password
>>> assert_equal 'newpassword', assigns[:user].password
>>> assert_equal 'newpassword', assigns['user'].password
>>>

But did you try:
assert_equal 'newpassword', assigns(:user).password

and you actually do assign to @user in the controller, right?

-Rob

Rob Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com

>>
>> Are you using fixtures for your tests? (I assume you are, from the
>> assigns() usage).
>>
>> Can you post the fixtures?
>>
>>> Thanks!
>> You're welcome
>
> the fixtures file is:
> user1:
> id: 1
> login: testuser1
> user_name:asb
> password: 123
> created_at: 2006-04-12 14:30:25
> user2:
> id: 2
> login: testuser2
> user_name:asb123
> password: 123
> created_at: 2006-04-12 14:30:25


James Hunt

9/9/2007 9:42:00 PM

0

> But did you try:
> assert_equal 'newpassword', assigns(:user).password
>
> and you actually do assign to @user in the controller, right?
>
> -Rob
>
> Rob Biedenharn http://agileconsult...
> Rob@AgileConsultingLLC.com
>

I can't believe I completely missed the point of this post. No more
posting on < 3 hours of sleep :-D
--
James