我有这个任务模型:
class Task < ActiveRecord::Base acts_as_tree :order => 'sort_order' end
我有这个测试
class TaskTest < Test::Unit::TestCase def setup @root = create_root end def test_destroying_a_task_should_destroy_all_of_its_descendants d1 = create_task(:parent_id => @root.id, :sort_order => 2) d2 = create_task(:parent_id => d1.id, :sort_order => 3) d3 = create_task(:parent_id => d2.id, :sort_order => 4) d4 = create_task(:parent_id => d1.id, :sort_order => 5) assert_equal 5, Task.count d1.destroy assert_equal @root, Task.find(:first) assert_equal 1, Task.count end end
测试成功:当我销毁d1时,它会破坏d1的所有后代.因此,在破坏之后只剩下根.
但是,在我向Task添加了before_save回调之后,此测试现在失败了.这是我添加到Task的代码:
before_save :update_descendants_if_necessary def update_descendants_if_necessary handle_parent_id_change if self.parent_id_changed? return true end def handle_parent_id_change self.children.each do |sub_task| #the code within the loop is deliberately commented out end end
当我添加此代码时,assert_equal 1, Task.count
失败,用Task.count == 4
.我认为self.children
下面handled_parent_id_change
是罪魁祸首,因为当我注释掉该self.children.each do |sub_task|
块时,测试再次通过.
有任何想法吗?