diff --git a/test/unit/associations_test.rb b/test/unit/associations_test.rb index dec83788f..8839e90a5 100644 --- a/test/unit/associations_test.rb +++ b/test/unit/associations_test.rb @@ -776,16 +776,16 @@ class AssociationsTest < ActiveSupport::TestCase context "where the association is created between model versions" do setup do - @wotsit = @widget.create_wotsit(name: "wotsit_0") + @wotsit = Wotsit.create(name: "wotsit_0") Timecop.travel 1.second.since - @wotsit.update_attributes name: "wotsit_1" + @wotsit.update_attributes widget_id: @widget.id, name: "wotsit_1" end context "when reified" do setup { @wotsit_0 = @wotsit.versions.last.reify(belongs_to: true) } should "see the associated as it was at the time" do - assert_equal "widget_0", @wotsit_0.widget.name + assert_equal nil, @wotsit_0.widget end should "not persist changes to the live association" do @@ -824,13 +824,16 @@ class AssociationsTest < ActiveSupport::TestCase end context "and then the associated is destroyed" do - setup { @widget.destroy } + setup do + @wotsit.update_attributes name: "wotsit_2" + @widget.destroy + end - context "when reify" do - setup { @wotsit_1 = @wotsit.versions.last.reify(belongs_to: true) } + context "when reified" do + setup { @wotsit_2 = @wotsit.versions.last.reify(belongs_to: true) } should "see the associated as it was at the time" do - assert_equal @widget, @wotsit_1.widget + assert_equal @widget, @wotsit_2.widget end should "not persist changes to the live association" do @@ -854,6 +857,38 @@ class AssociationsTest < ActiveSupport::TestCase end end end + + context "where the association is changed between model versions" do + setup do + @wotsit = @widget.create_wotsit(name: "wotsit_0") + Timecop.travel 1.second.since + @new_widget = Widget.create(name: "new_widget") + @wotsit.update_attributes(widget_id: @new_widget.id, name: "wotsit_1") + end + + context "when reified" do + setup { @wotsit_0 = @wotsit.versions.last.reify(belongs_to: true) } + + should "see the association as it was at the time" do + assert_equal "widget_0", @wotsit_0.widget.name + end + + should "not persist changes to the live association" do + assert_equal @new_widget, @wotsit.reload.widget + end + end + + context "when reified with option mark_for_destruction" do + setup do + @wotsit_0 = @wotsit.versions.last. + reify(belongs_to: true, mark_for_destruction: true) + end + + should "should not mark the new associated for destruction" do + assert_equal false, @new_widget.marked_for_destruction? + end + end + end end end end