Skip to content

Commit 349ed43

Browse files
authored
Merge pull request rails#48410 from Shopify/get-rid-of-custom-assert-raises-with-message
Use native `assert_raises` instead of custom `assert_raises_with_message`
2 parents 791f109 + e583f15 commit 349ed43

File tree

3 files changed

+19
-38
lines changed

3 files changed

+19
-38
lines changed

activerecord/test/cases/adapters/trilogy/trilogy_adapter_test.rb

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class TrilogyAdapterTest < ActiveRecord::TrilogyTestCase
137137
end
138138

139139
test "#exec_query fails with invalid query" do
140-
error = assert_raises_with_message ActiveRecord::StatementInvalid, /'activerecord_unittest.bogus' doesn't exist/ do
140+
error = assert_raises ActiveRecord::StatementInvalid, match: /'activerecord_unittest.bogus' doesn't exist/ do
141141
@conn.exec_query "SELECT * FROM bogus;"
142142
end
143143
assert_equal @conn.pool, error.connection_pool
@@ -149,7 +149,7 @@ class TrilogyAdapterTest < ActiveRecord::TrilogyTestCase
149149
end
150150

151151
test "#execute fails with invalid query" do
152-
error = assert_raises_with_message ActiveRecord::StatementInvalid, /Table 'activerecord_unittest.bogus' doesn't exist/ do
152+
error = assert_raises ActiveRecord::StatementInvalid, match: /Table 'activerecord_unittest.bogus' doesn't exist/ do
153153
@conn.execute "SELECT * FROM bogus;"
154154
end
155155
assert_equal @conn.pool, error.connection_pool
@@ -351,15 +351,6 @@ class TrilogyAdapterTest < ActiveRecord::TrilogyTestCase
351351
ActiveRecord::Base.establish_connection :arunit
352352
end
353353

354-
def assert_raises_with_message(exception, message, &block)
355-
block.call
356-
rescue exception => error
357-
assert_match message, error.message
358-
error
359-
else
360-
fail %(Expected #{exception} with message "#{message}" but nothing failed.)
361-
end
362-
363354
# Create a temporary subscription to verify notification is sent.
364355
# Optionally verify the notification payload includes expected types.
365356
def assert_notification(notification, expected_payload = {}, &block)

activerecord/test/cases/finder_test.rb

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ def test_take_bang_present
667667
end
668668

669669
def test_take_bang_missing
670-
assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
670+
assert_raises ActiveRecord::RecordNotFound, match: "Couldn't find Topic" do
671671
Topic.where("title = 'This title does not exist'").take!
672672
end
673673
end
@@ -678,19 +678,19 @@ def test_sole
678678
end
679679

680680
def test_sole_failing_none
681-
assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
681+
assert_raises ActiveRecord::RecordNotFound, match: "Couldn't find Topic" do
682682
Topic.where("title = 'This title does not exist'").sole
683683
end
684-
assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
684+
assert_raises ActiveRecord::RecordNotFound, match: "Couldn't find Topic" do
685685
Topic.find_sole_by("title = 'This title does not exist'")
686686
end
687687
end
688688

689689
def test_sole_failing_many
690-
assert_raises_with_message ActiveRecord::SoleRecordExceeded, "Wanted only one Topic" do
690+
assert_raises ActiveRecord::SoleRecordExceeded, match: "Wanted only one Topic" do
691691
Topic.where("author_name = 'Carl'").sole
692692
end
693-
assert_raises_with_message ActiveRecord::SoleRecordExceeded, "Wanted only one Topic" do
693+
assert_raises ActiveRecord::SoleRecordExceeded, match: "Wanted only one Topic" do
694694
Topic.find_sole_by("author_name = 'Carl'")
695695
end
696696
end
@@ -710,7 +710,7 @@ def test_first_bang_present
710710
end
711711

712712
def test_first_bang_missing
713-
assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
713+
assert_raises ActiveRecord::RecordNotFound, match: "Couldn't find Topic" do
714714
Topic.where("title = 'This title does not exist'").first!
715715
end
716716
end
@@ -726,7 +726,7 @@ def test_first_have_primary_key_order_by_default
726726
def test_model_class_responds_to_first_bang
727727
assert Topic.first!
728728
Topic.delete_all
729-
assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
729+
assert_raises ActiveRecord::RecordNotFound, match: "Couldn't find Topic" do
730730
Topic.first!
731731
end
732732
end
@@ -750,7 +750,7 @@ def test_second_have_primary_key_order_by_default
750750
def test_model_class_responds_to_second_bang
751751
assert Topic.second!
752752
Topic.delete_all
753-
assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
753+
assert_raises ActiveRecord::RecordNotFound, match: "Couldn't find Topic" do
754754
Topic.second!
755755
end
756756
end
@@ -774,7 +774,7 @@ def test_third_have_primary_key_order_by_default
774774
def test_model_class_responds_to_third_bang
775775
assert Topic.third!
776776
Topic.delete_all
777-
assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
777+
assert_raises ActiveRecord::RecordNotFound, match: "Couldn't find Topic" do
778778
Topic.third!
779779
end
780780
end
@@ -798,7 +798,7 @@ def test_fourth_have_primary_key_order_by_default
798798
def test_model_class_responds_to_fourth_bang
799799
assert Topic.fourth!
800800
Topic.delete_all
801-
assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
801+
assert_raises ActiveRecord::RecordNotFound, match: "Couldn't find Topic" do
802802
Topic.fourth!
803803
end
804804
end
@@ -822,7 +822,7 @@ def test_fifth_have_primary_key_order_by_default
822822
def test_model_class_responds_to_fifth_bang
823823
assert Topic.fifth!
824824
Topic.delete_all
825-
assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
825+
assert_raises ActiveRecord::RecordNotFound, match: "Couldn't find Topic" do
826826
Topic.fifth!
827827
end
828828
end
@@ -851,7 +851,7 @@ def test_second_to_last_have_primary_key_order_by_default
851851
def test_model_class_responds_to_second_to_last_bang
852852
assert Topic.second_to_last!
853853
Topic.delete_all
854-
assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
854+
assert_raises ActiveRecord::RecordNotFound, match: "Couldn't find Topic" do
855855
Topic.second_to_last!
856856
end
857857
end
@@ -882,7 +882,7 @@ def test_third_to_last_have_primary_key_order_by_default
882882
def test_model_class_responds_to_third_to_last_bang
883883
assert Topic.third_to_last!
884884
Topic.delete_all
885-
assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
885+
assert_raises ActiveRecord::RecordNotFound, match: "Couldn't find Topic" do
886886
Topic.third_to_last!
887887
end
888888
end
@@ -905,14 +905,14 @@ def test_last_bang_present
905905
end
906906

907907
def test_last_bang_missing
908-
assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
908+
assert_raises ActiveRecord::RecordNotFound, match: "Couldn't find Topic" do
909909
Topic.where("title = 'This title does not exist'").last!
910910
end
911911
end
912912

913913
def test_model_class_responds_to_last_bang
914914
assert_equal topics(:fifth), Topic.last!
915-
assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
915+
assert_raises ActiveRecord::RecordNotFound, match: "Couldn't find Topic" do
916916
Topic.delete_all
917917
Topic.last!
918918
end
@@ -1390,7 +1390,7 @@ def test_find_by_one_attribute
13901390

13911391
def test_find_by_one_attribute_bang
13921392
assert_equal topics(:first), Topic.find_by_title!("The First Topic")
1393-
assert_raises_with_message(ActiveRecord::RecordNotFound, "Couldn't find Topic") do
1393+
assert_raises ActiveRecord::RecordNotFound, match: "Couldn't find Topic" do
13941394
Topic.find_by_title!("The First Topic!")
13951395
end
13961396
end
@@ -1823,9 +1823,4 @@ def self.name
18231823
end
18241824
end)
18251825
end
1826-
1827-
def assert_raises_with_message(exception_class, message, &block)
1828-
err = assert_raises(exception_class) { block.call }
1829-
assert_match message, err.message
1830-
end
18311826
end

activesupport/test/core_ext/object/json_gem_encoding_test.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,11 @@ def assert_same_with_or_without_active_support(subject)
5353
require_or_skip "active_support/core_ext/object/json"
5454

5555
if exception
56-
assert_raises_with_message JSON::GeneratorError, e.message do
56+
assert_raises JSON::GeneratorError, match: e.message do
5757
JSON.generate(subject, quirks_mode: true)
5858
end
5959
else
6060
assert_equal expected, JSON.generate(subject, quirks_mode: true)
6161
end
6262
end
63-
64-
def assert_raises_with_message(exception_class, message, &block)
65-
err = assert_raises(exception_class) { block.call }
66-
assert_match message, err.message
67-
end
6863
end

0 commit comments

Comments
 (0)