提交 577d83a5 编写于 作者: R Ryuta Kamizono

More exercise `ActiveModel::Dirty` tests

上级 05baed07
......@@ -5,12 +5,13 @@
class DirtyTest < ActiveModel::TestCase
class DirtyModel
include ActiveModel::Dirty
define_attribute_methods :name, :color, :size
define_attribute_methods :name, :color, :size, :status
def initialize
@name = nil
@color = nil
@size = nil
@status = "initialized"
end
def name
......@@ -40,6 +41,15 @@ def size=(val)
@size = val
end
def status
@status
end
def status=(val)
status_will_change! unless val == @status
@status = val
end
def save
changes_applied
end
......@@ -135,15 +145,20 @@ def reload
test "saving should preserve previous changes" do
@model.name = "Jericho Cane"
@model.status = "waiting"
@model.save
assert_equal [nil, "Jericho Cane"], @model.previous_changes["name"]
assert_equal ["initialized", "waiting"], @model.previous_changes["status"]
end
test "setting new attributes should not affect previous changes" do
@model.name = "Jericho Cane"
@model.status = "waiting"
@model.save
@model.name = "DudeFella ManGuy"
@model.status = "finished"
assert_equal [nil, "Jericho Cane"], @model.name_previous_change
assert_equal ["initialized", "waiting"], @model.previous_changes["status"]
end
test "saving should preserve model's previous changed status" do
......@@ -155,20 +170,26 @@ def reload
test "previous value is preserved when changed after save" do
assert_equal({}, @model.changed_attributes)
@model.name = "Paul"
assert_equal({ "name" => nil }, @model.changed_attributes)
@model.status = "waiting"
assert_equal({ "name" => nil, "status" => "initialized" }, @model.changed_attributes)
@model.save
@model.name = "John"
assert_equal({ "name" => "Paul" }, @model.changed_attributes)
@model.status = "finished"
assert_equal({ "name" => "Paul", "status" => "waiting" }, @model.changed_attributes)
end
test "changing the same attribute multiple times retains the correct original value" do
@model.name = "Otto"
@model.status = "waiting"
@model.save
@model.name = "DudeFella ManGuy"
@model.name = "Mr. Manfredgensonton"
@model.status = "processing"
@model.status = "finished"
assert_equal ["Otto", "Mr. Manfredgensonton"], @model.name_change
assert_equal ["waiting", "finished"], @model.status_change
assert_equal @model.name_was, "Otto"
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册