Testing Problem


#1

I’m trying to test code in a controller that needs to respond to changes
in the database, but I’m having problems updating the model from the
functional test. Here’s a rough approximation of what I have:

def test_changes
get :list
assert_equal old_price = product(:myproduct).price,
assigns(:items)[0].price
Product.find(product(:myproduct).id).update_attribute(‘price’,
old_price + 100)
get :list
assert_equal old_price + 100, assigns(:items)[0].price
end

I can also try Product.find(1) (the fixture has an id of 1), and try
separating out .price += 100 and .save, with the same error happening on
the line with .save.

This is the error I’m getting:

  1. Error:
    test_changes(MyControllerTest):
    TypeError: cannot convert String into Integer
    /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/associations/association_proxy.rb:75:in
    []' /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/associations/association_proxy.rb:75:insend’
    /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/associations/association_proxy.rb:75:in
    method_missing' /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/associations/has_and_belongs_to_many_association.rb:81:inmethod_missing’
    (eval):1:in id' /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/base.rb:1439:inupdate_without_lock’
    /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/locking.rb:45:in
    update_without_callbacks' /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/callbacks.rb:274:inupdate_without_timestamps’
    /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/timestamp.rb:39:in
    update' /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/base.rb:1431:increate_or_update_without_callbacks’
    /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/callbacks.rb:249:in
    create_or_update' /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/base.rb:1231:insave_without_validation’
    /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/validations.rb:687:in
    save_without_transactions' /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/transactions.rb:126:insave’
    /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/transactions.rb:126:in
    transaction' /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/transactions.rb:91:intransaction’
    /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/transactions.rb:118:in
    transaction' /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/transactions.rb:126:insave’
    /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/validations.rb:709:in
    `update_attribute’
    ./test/functional/my_controller_test.rb:XXX:in ‘test_changes’

Any help solving this would be greatly appreciated!


#2

Just a thought, but did you try ensurnig that old_price was an integer?

Product.find(product(:myproduct).id).update_attribute(‘price’,
old_price.to_i + 100)

or even…

Product.find(product(:myproduct).id).update_attribute(‘price’,
old_price.to_f + 100)


#3

Actually, it was a mistake I made somewhere else, completely unrelated.
The problem was all me. :slight_smile: