diff options
author | Murray Steele <muz@h-lame.com> | 2011-03-11 11:41:30 +0000 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2011-03-23 14:45:21 -0700 |
commit | 8ee81d21fb103be31adb8e0dcde8ed8f5e90a798 (patch) | |
tree | 155160de598c841ef2acf995f666f49ac82a3be6 | |
parent | 3b91b3726dbd7ef6e1f4927d65e2cbe8da12f7c7 (diff) | |
download | rails-8ee81d21fb103be31adb8e0dcde8ed8f5e90a798.tar.gz rails-8ee81d21fb103be31adb8e0dcde8ed8f5e90a798.tar.bz2 rails-8ee81d21fb103be31adb8e0dcde8ed8f5e90a798.zip |
Failing test case to show that habtm join table contents are removed when a model is destroyed but the destruction is blocked by a before_destroy.
-rw-r--r-- | activerecord/test/cases/habtm_destroy_order_test.rb | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/activerecord/test/cases/habtm_destroy_order_test.rb b/activerecord/test/cases/habtm_destroy_order_test.rb index 15598392e2..f2b91d977e 100644 --- a/activerecord/test/cases/habtm_destroy_order_test.rb +++ b/activerecord/test/cases/habtm_destroy_order_test.rb @@ -13,5 +13,39 @@ class HabtmDestroyOrderTest < ActiveRecord::TestCase sicp.destroy end end + assert !sicp.destroyed? + end + + test "not destroying a student with lessons leaves student<=>lesson association intact" do + # test a normal before_destroy doesn't destroy the habtm joins + begin + sicp = Lesson.new(:name => "SICP") + ben = Student.new(:name => "Ben Bitdiddle") + # add a before destroy to student + Student.class_eval do + before_destroy do + raise ActiveRecord::Rollback unless lessons.empty? + end + end + ben.lessons << sicp + ben.save! + ben.destroy + assert !ben.reload.lessons.empty? + ensure + # get rid of it so Student is still like it was + Student.reset_callbacks(:destroy) + end + end + + test "not destroying a lesson with students leaves student<=>lesson association intact" do + # test a more aggressive before_destroy doesn't destroy the habtm joins and still throws the exception + sicp = Lesson.new(:name => "SICP") + ben = Student.new(:name => "Ben Bitdiddle") + sicp.students << ben + sicp.save! + assert_raises LessonError do + sicp.destroy + end + assert !sicp.reload.students.empty? end end |