After_create callback does not work in the test, but works in the console

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 # this passes
  absence = user.absences.create(:from => "2011-12-02", :to => "2011-12-03", :category_id => 1, :employee_notes => "Secret") # this works
  assert_equal user.holidays_booked_this_year, 5 # fails
end

absence.rb

after_create :update_holidays_booked

def update_holidays_booked
  user = self.user
  user.holidays_booked_this_year += self.days_used # the days used attribute is calculated using a before_create callback on the absence
  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

+3
source share
1 answer

factory?

, ( , ), :

assert_equal user.reload.holidays_booked_this_year, 5

, , , "" :

user.absences.build

, , . ?:

raise user.inspect

, , holidays_booked_this_year.

( "" )

+4
source

All Articles