【发布时间】:2011-03-15 08:56:39
【问题描述】:
在 Rails 中进行测试一直是个谜,如果可能的话我会尽量避免,但我正在将一个生产应用程序放在一起,人们会为此付费,所以我真的需要测试。这个问题让我很生气,因为测试失败但是当我在控制台中执行相同的命令时(在测试和开发模式下)它工作正常。
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
缺席.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
我唯一的想法是,这与通过 Absence 模型上的回调更新 User 模型有关,但正如我所说,这在控制台中有效。
任何建议将不胜感激。
谢谢
罗宾
【问题讨论】:
标签: ruby-on-rails unit-testing activerecord callback