提交 f734ec48 编写于 作者: A Aaron Patterson

define singleton methods rather than adding and removing methods

上级 79d21ddd
......@@ -121,50 +121,39 @@ def test_update_attributes_should_rollback_on_failure!
end
def test_cancellation_from_before_destroy_rollbacks_in_destroy
add_cancelling_before_destroy_with_db_side_effect_to_topic
begin
nbooks_before_destroy = Book.count
status = @first.destroy
assert !status
assert_nothing_raised(ActiveRecord::RecordNotFound) { @first.reload }
assert_equal nbooks_before_destroy, Book.count
ensure
remove_cancelling_before_destroy_with_db_side_effect_to_topic
end
add_cancelling_before_destroy_with_db_side_effect_to_topic @first
nbooks_before_destroy = Book.count
status = @first.destroy
assert !status
@first.reload
assert_equal nbooks_before_destroy, Book.count
end
def test_cancellation_from_before_filters_rollbacks_in_save
%w(validation save).each do |filter|
send("add_cancelling_before_#{filter}_with_db_side_effect_to_topic")
begin
nbooks_before_save = Book.count
original_author_name = @first.author_name
@first.author_name += '_this_should_not_end_up_in_the_db'
status = @first.save
assert !status
assert_equal original_author_name, @first.reload.author_name
assert_equal nbooks_before_save, Book.count
ensure
send("remove_cancelling_before_#{filter}_with_db_side_effect_to_topic")
end
%w(validation save).each do |filter|
define_method("test_cancellation_from_before_filters_rollbacks_in_#{filter}") do
send("add_cancelling_before_#{filter}_with_db_side_effect_to_topic", @first)
nbooks_before_save = Book.count
original_author_name = @first.author_name
@first.author_name += '_this_should_not_end_up_in_the_db'
status = @first.save
assert !status
assert_equal original_author_name, @first.reload.author_name
assert_equal nbooks_before_save, Book.count
end
end
def test_cancellation_from_before_filters_rollbacks_in_save!
%w(validation save).each do |filter|
send("add_cancelling_before_#{filter}_with_db_side_effect_to_topic")
define_method("test_cancellation_from_before_filters_rollbacks_in_#{filter}!") do
send("add_cancelling_before_#{filter}_with_db_side_effect_to_topic", @first)
nbooks_before_save = Book.count
original_author_name = @first.author_name
@first.author_name += '_this_should_not_end_up_in_the_db'
begin
nbooks_before_save = Book.count
original_author_name = @first.author_name
@first.author_name += '_this_should_not_end_up_in_the_db'
@first.save!
flunk
rescue
assert_equal original_author_name, @first.reload.author_name
assert_equal nbooks_before_save, Book.count
ensure
send("remove_cancelling_before_#{filter}_with_db_side_effect_to_topic")
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved
end
assert_equal original_author_name, @first.reload.author_name
assert_equal nbooks_before_save, Book.count
end
end
......@@ -475,14 +464,12 @@ def define_callback_method(callback_method)
end
%w(validation save destroy).each do |filter|
define_method("add_cancelling_before_#{filter}_with_db_side_effect_to_topic") do
Topic.class_eval <<-eoruby, __FILE__, __LINE__ + 1
remove_method :before_#{filter}_for_transaction
def before_#{filter}_for_transaction
Book.create
false
end
eoruby
define_method("add_cancelling_before_#{filter}_with_db_side_effect_to_topic") do |topic|
meta = class << topic; self; end
meta.send("define_method", "before_#{filter}_for_transaction") do
Book.create
false
end
end
define_method("remove_cancelling_before_#{filter}_with_db_side_effect_to_topic") do
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册