Forum: Ruby on Rails Unit test failure - nil object?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Lindsay B. (Guest)
on 2006-02-22 14:32
I'm following instructions in the Agile book, page 146 for testing an
object update to the database. Here's my code:

  def test_update
    assert_equal "removed_email_address@domain.invalid", @user.email
    @user.email = "removed_email_address@domain.invalid"
    assert @user.save, @user.errors.full_messages.join("; ")
    @user.reload
    assert_equal @user.email, "removed_email_address@domain.invalid"
  end

An error is generated when the save method is called. I'm guessing the
code is OK and the problem lies with the database? Here's the error
text:

  1) Error:
test_update(UserTest):
NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.+
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/locking.rb:33:in
`update_without_callbacks'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/callbacks.rb:274:in
`update_without_timestamps'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/timestamp.rb:39:in
`update'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1431:in
`create_or_update_without_callbacks'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/callbacks.rb:249:in
`create_or_update'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1226:in
`save_without_validation'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/validations.rb:698:in
`save_without_transactions'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/transactions.rb:126:in
`save'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/connection_adapters/abstract/database_statements.rb:51:in
`transaction'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/transactions.rb:91:in
`transaction'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/transactions.rb:118:in
`transaction'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/transactions.rb:126:in
`save'
    test/unit/user_test.rb:31:in `test_update'

2 tests, 16 assertions, 0 failures, 1 errors
Lindsay B. (Guest)
on 2006-02-22 14:35
Arrgh! Here's the actual code I'm testing:

>   def test_update
>     assert_equal "removed_email_address@domain.invalid", @user.email
>     @user.email = "removed_email_address@domain.invalid"
>     assert @user.save, @user.errors.full_messages.join("; ")
>     @user.reload
>     assert_equal @user.email, "removed_email_address@domain.invalid"
>   end

So, email removed_email_address@domain.invalid -> 
removed_email_address@domain.invalid, but as I say, the error is
with the save method.
Luke R. (Guest)
on 2006-02-22 19:10
(Received via mailing list)
The error probably relates to you calling join() on
@user.errors.full_messages, not the save call.

Before that line, add a new assertion

assert_instance_of Array, @user.errors.full_messages

If that fails, then that is your problem.

On 2/22/06, Lindsay B. <removed_email_address@domain.invalid> wrote:
> So, email removed_email_address@domain.invalid -> removed_email_address@domain.invalid, 
but as I say, the error is
> with the save method.
>
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
Cheers,
Luke R.
www.lukeredpath.co.uk
Lindsay B. (Guest)
on 2006-02-22 23:14
That's it! Thanks.
This topic is locked and can not be replied to.