Topic: Cant make my test to work...
I have this class:
class ForSale < ActiveRecord::Base
belongs_to :user
def sold!(sold_at = DateTime.now)
if self.sold_on.nil?
self.sold_on = sold_at
self.save
user.reload #<-------this doesnt work????
else
false #add error message
end
end
end
and this unit test:
def test_sold
guy = users(:guy)
rebel= for_sales(:guy_rebel)
assert_equal 0, guy.sales.size
rebel.sold!
#guy.reload <--- this is the problematic line!!!!
assert_equal 1, guy.sales.size
end
My last assertion doesnt work unless I reload the variable (commented line). So here are my questions:
- is there a better way to do the sold! function?
- why the user.reload code doesnt seem to work, but if I call reload on the User object (guy) it works.
- logicaly, keeping the User up to date when an item is sold should be the job of the ForSale class. So how can I do this?
Thanks a lot!