mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
define singleton methods rather than adding and removing methods
This commit is contained in:
parent
79d21dddd6
commit
f734ec4807
1 changed files with 32 additions and 45 deletions
|
@ -121,50 +121,39 @@ class TransactionTest < ActiveRecord::TestCase
|
|||
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
|
||||
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
|
||||
|
||||
%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_nothing_raised(ActiveRecord::RecordNotFound) { @first.reload }
|
||||
assert_equal nbooks_before_destroy, Book.count
|
||||
ensure
|
||||
remove_cancelling_before_destroy_with_db_side_effect_to_topic
|
||||
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")
|
||||
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
|
||||
end
|
||||
end
|
||||
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'
|
||||
|
||||
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'
|
||||
@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 @@ class TransactionTest < ActiveRecord::TestCase
|
|||
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
|
||||
|
|
Loading…
Reference in a new issue