Testing in Rails has always been a mystery, which I avoid if possible, but I am putting together a production application that people will pay for, so I really need to test. This problem drives me crazy because the test fails, but when I execute the same commands on the console (in test and development mode), it works fine.
user_test.rb
test "should update holidays booked after create"
user = users(:robin)
assert_equal user.holidays_booked_this_year, 4
absence = user.absences.create(:from => "2011-12-02", :to => "2011-12-03", :category_id => 1, :employee_notes => "Secret")
assert_equal user.holidays_booked_this_year, 5
end
absence.rb
after_create :update_holidays_booked
def update_holidays_booked
user = self.user
user.holidays_booked_this_year += self.days_used
user.save
end
My only thoughts are that it has something to do with updating the User model through a callback for the Absence model, but as I said, this works in the console.
Any advice would be appreciated.
thank
Robin
source
share