Unit test failure - nil object?


#1

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:inupdate_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:increate_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:insave_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:insave’
    /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:intransaction’
    /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:insave’
    test/unit/user_test.rb:31:in `test_update’

2 tests, 16 assertions, 0 failures, 1 errors


#2

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.


#3

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


#4

That’s it! Thanks.