@@ -538,15 +538,27 @@ def test_adding_a_collection
538538 end
539539
540540 def test_transactions_when_adding_to_persisted
541- force_signal37_to_load_all_clients_of_firm
542- Client . expects ( :transaction )
543- companies ( :first_firm ) . clients_of_firm . concat ( Client . new ( "name" => "Natural Company" ) )
541+ good = Client . new ( :name => "Good" )
542+ bad = Client . new ( :name => "Bad" , :raise_on_save => true )
543+
544+ begin
545+ companies ( :first_firm ) . clients_of_firm . concat ( good , bad )
546+ rescue Client ::RaisedOnSave
547+ end
548+
549+ assert !companies ( :first_firm ) . clients_of_firm ( true ) . include? ( good )
544550 end
545551
546552 def test_transactions_when_adding_to_new_record
547- Client . expects ( :transaction ) . never
548- firm = Firm . new
549- firm . clients_of_firm . concat ( Client . new ( "name" => "Natural Company" ) )
553+ prev_ignored_sql = ActiveRecord ::SQLCounter . ignored_sql
554+ ActiveRecord ::SQLCounter . ignored_sql = [ ]
555+
556+ assert_no_queries do
557+ firm = Firm . new
558+ firm . clients_of_firm . concat ( Client . new ( "name" => "Natural Company" ) )
559+ end
560+ ensure
561+ ActiveRecord ::SQLCounter . ignored_sql = prev_ignored_sql
550562 end
551563
552564 def test_new_aliased_to_build
@@ -791,18 +803,31 @@ def test_delete_all_with_not_yet_loaded_association_collection
791803 end
792804
793805 def test_transaction_when_deleting_persisted
794- force_signal37_to_load_all_clients_of_firm
795- client = companies ( :first_firm ) . clients_of_firm . create ( "name" => "Another Client" )
796- Client . expects ( :transaction )
797- companies ( :first_firm ) . clients_of_firm . delete ( client )
806+ good = Client . new ( :name => "Good" )
807+ bad = Client . new ( :name => "Bad" , :raise_on_destroy => true )
808+
809+ companies ( :first_firm ) . clients_of_firm = [ good , bad ]
810+
811+ begin
812+ companies ( :first_firm ) . clients_of_firm . destroy ( good , bad )
813+ rescue Client ::RaisedOnDestroy
814+ end
815+
816+ assert_equal [ good , bad ] , companies ( :first_firm ) . clients_of_firm ( true )
798817 end
799818
800819 def test_transaction_when_deleting_new_record
801- client = Client . new ( "name" => "New Client" )
802- firm = Firm . new
803- firm . clients_of_firm << client
804- Client . expects ( :transaction ) . never
805- firm . clients_of_firm . delete ( client )
820+ prev_ignored_sql = ActiveRecord ::SQLCounter . ignored_sql
821+ ActiveRecord ::SQLCounter . ignored_sql = [ ]
822+
823+ assert_no_queries do
824+ firm = Firm . new
825+ client = Client . new ( "name" => "New Client" )
826+ firm . clients_of_firm << client
827+ firm . clients_of_firm . destroy ( client )
828+ end
829+ ensure
830+ ActiveRecord ::SQLCounter . ignored_sql = prev_ignored_sql
806831 end
807832
808833 def test_clearing_an_association_collection
@@ -1139,21 +1164,29 @@ def test_replace_failure
11391164 end
11401165
11411166 def test_transactions_when_replacing_on_persisted
1142- firm = Firm . find ( :first , :order => "id" )
1143- firm . clients = [ companies ( :first_client ) ]
1144- assert firm . save , "Could not save firm"
1145- firm . reload
1167+ good = Client . new ( :name => "Good" )
1168+ bad = Client . new ( :name => "Bad" , :raise_on_save => true )
11461169
1147- Client . expects ( :transaction )
1148- firm . clients_of_firm = [ Client . new ( "name" => "Natural Company" ) ]
1170+ companies ( :first_firm ) . clients_of_firm = [ good ]
1171+
1172+ begin
1173+ companies ( :first_firm ) . clients_of_firm = [ bad ]
1174+ rescue Client ::RaisedOnSave
1175+ end
1176+
1177+ assert_equal [ good ] , companies ( :first_firm ) . clients_of_firm ( true )
11491178 end
11501179
11511180 def test_transactions_when_replacing_on_new_record
1152- firm = Firm . new
1153- firm . clients_of_firm << Client . new ( "name" => "Natural Company" )
1181+ prev_ignored_sql = ActiveRecord :: SQLCounter . ignored_sql
1182+ ActiveRecord :: SQLCounter . ignored_sql = [ ]
11541183
1155- Client . expects ( :transaction ) . never
1156- firm . clients_of_firm = [ Client . new ( "name" => "New Client" ) ]
1184+ assert_no_queries do
1185+ firm = Firm . new
1186+ firm . clients_of_firm = [ Client . new ( "name" => "New Client" ) ]
1187+ end
1188+ ensure
1189+ ActiveRecord ::SQLCounter . ignored_sql = prev_ignored_sql
11571190 end
11581191
11591192 def test_get_ids
0 commit comments