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 "jack@smiths.com", @user.email @user.email = "jack@smiths.org" assert @user.save, @user.errors.full_messages.join("; ") @user.reload assert_equal @user.email, "lindsay.boyd@ntlworld.co.uk" 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 -- Posted via http://www.ruby-forum.com/.
Arrgh! Here''s the actual code I''m testing:> def test_update > assert_equal "jack@smiths.com", @user.email > @user.email = "jack@smiths.org" > assert @user.save, @user.errors.full_messages.join("; ") > @user.reload > assert_equal @user.email, "jack@smiths.org" > endSo, email jack@smiths.com -> jack@smiths.org, but as I say, the error is with the save method. -- Posted via http://www.ruby-forum.com/.
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 Boyd <lindsay.boyd@ntlworld.com> wrote:> Arrgh! Here''s the actual code I''m testing: > > > def test_update > > assert_equal "jack@smiths.com", @user.email > > @user.email = "jack@smiths.org" > > assert @user.save, @user.errors.full_messages.join("; ") > > @user.reload > > assert_equal @user.email, "jack@smiths.org" > > end > > So, email jack@smiths.com -> jack@smiths.org, but as I say, the error is > with the save method. > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Cheers, Luke Redpath www.lukeredpath.co.uk