How to fix assigns(:user) on controller test

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-forum.com/\.

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

···

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

--
James

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://agileconsultingllc.com
Rob@AgileConsultingLLC.com

···

On Sep 9, 2007, at 4:56 PM, OnRails Ruby wrote:

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

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://agileconsultingllc.com
Rob@AgileConsultingLLC.com

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

···

--
James