Forum: Ruby on Rails Testing Problem

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.
Db303dc84d03a992b33cd3ac978f89ae?d=identicon&s=25 rails (Guest)
on 2005-12-07 19:42
(Received via mailing list)
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:in
`send'
    /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:in
`method_missing'
    (eval):1:in `id'
    /usr/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/base.rb:1439:in
`update_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:in
`update_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:in
`create_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:in
`save_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:in
`save'
    /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:in
`transaction'
    /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:in
`save'
    /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!
55742d912a9003080d202c5522d4de00?d=identicon&s=25 rails (Guest)
on 2005-12-07 21:20
(Received via mailing list)
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)
Db303dc84d03a992b33cd3ac978f89ae?d=identicon&s=25 rails (Guest)
on 2005-12-07 21:49
(Received via mailing list)
Actually, it was a mistake I made somewhere else, completely unrelated.
The problem was all me. :)
This topic is locked and can not be replied to.