Skip to content

Commit

Permalink
Merge pull request rails#48410 from Shopify/get-rid-of-custom-assert-…
Browse files Browse the repository at this point in the history
…raises-with-message

Use native `assert_raises` instead of custom `assert_raises_with_message`
  • Loading branch information
gmcgibbon authored Jun 6, 2023
2 parents 791f109 + e583f15 commit 349ed43
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 38 deletions.
13 changes: 2 additions & 11 deletions activerecord/test/cases/adapters/trilogy/trilogy_adapter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class TrilogyAdapterTest < ActiveRecord::TrilogyTestCase
end

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

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

def assert_raises_with_message(exception, message, &block)
block.call
rescue exception => error
assert_match message, error.message
error
else
fail %(Expected #{exception} with message "#{message}" but nothing failed.)
end

# Create a temporary subscription to verify notification is sent.
# Optionally verify the notification payload includes expected types.
def assert_notification(notification, expected_payload = {}, &block)
Expand Down
37 changes: 16 additions & 21 deletions activerecord/test/cases/finder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ def test_take_bang_present
end

def test_take_bang_missing
assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
assert_raises ActiveRecord::RecordNotFound, match: "Couldn't find Topic" do
Topic.where("title = 'This title does not exist'").take!
end
end
Expand All @@ -678,19 +678,19 @@ def test_sole
end

def test_sole_failing_none
assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
assert_raises ActiveRecord::RecordNotFound, match: "Couldn't find Topic" do
Topic.where("title = 'This title does not exist'").sole
end
assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
assert_raises ActiveRecord::RecordNotFound, match: "Couldn't find Topic" do
Topic.find_sole_by("title = 'This title does not exist'")
end
end

def test_sole_failing_many
assert_raises_with_message ActiveRecord::SoleRecordExceeded, "Wanted only one Topic" do
assert_raises ActiveRecord::SoleRecordExceeded, match: "Wanted only one Topic" do
Topic.where("author_name = 'Carl'").sole
end
assert_raises_with_message ActiveRecord::SoleRecordExceeded, "Wanted only one Topic" do
assert_raises ActiveRecord::SoleRecordExceeded, match: "Wanted only one Topic" do
Topic.find_sole_by("author_name = 'Carl'")
end
end
Expand All @@ -710,7 +710,7 @@ def test_first_bang_present
end

def test_first_bang_missing
assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
assert_raises ActiveRecord::RecordNotFound, match: "Couldn't find Topic" do
Topic.where("title = 'This title does not exist'").first!
end
end
Expand All @@ -726,7 +726,7 @@ def test_first_have_primary_key_order_by_default
def test_model_class_responds_to_first_bang
assert Topic.first!
Topic.delete_all
assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
assert_raises ActiveRecord::RecordNotFound, match: "Couldn't find Topic" do
Topic.first!
end
end
Expand All @@ -750,7 +750,7 @@ def test_second_have_primary_key_order_by_default
def test_model_class_responds_to_second_bang
assert Topic.second!
Topic.delete_all
assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
assert_raises ActiveRecord::RecordNotFound, match: "Couldn't find Topic" do
Topic.second!
end
end
Expand All @@ -774,7 +774,7 @@ def test_third_have_primary_key_order_by_default
def test_model_class_responds_to_third_bang
assert Topic.third!
Topic.delete_all
assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
assert_raises ActiveRecord::RecordNotFound, match: "Couldn't find Topic" do
Topic.third!
end
end
Expand All @@ -798,7 +798,7 @@ def test_fourth_have_primary_key_order_by_default
def test_model_class_responds_to_fourth_bang
assert Topic.fourth!
Topic.delete_all
assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
assert_raises ActiveRecord::RecordNotFound, match: "Couldn't find Topic" do
Topic.fourth!
end
end
Expand All @@ -822,7 +822,7 @@ def test_fifth_have_primary_key_order_by_default
def test_model_class_responds_to_fifth_bang
assert Topic.fifth!
Topic.delete_all
assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
assert_raises ActiveRecord::RecordNotFound, match: "Couldn't find Topic" do
Topic.fifth!
end
end
Expand Down Expand Up @@ -851,7 +851,7 @@ def test_second_to_last_have_primary_key_order_by_default
def test_model_class_responds_to_second_to_last_bang
assert Topic.second_to_last!
Topic.delete_all
assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
assert_raises ActiveRecord::RecordNotFound, match: "Couldn't find Topic" do
Topic.second_to_last!
end
end
Expand Down Expand Up @@ -882,7 +882,7 @@ def test_third_to_last_have_primary_key_order_by_default
def test_model_class_responds_to_third_to_last_bang
assert Topic.third_to_last!
Topic.delete_all
assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
assert_raises ActiveRecord::RecordNotFound, match: "Couldn't find Topic" do
Topic.third_to_last!
end
end
Expand All @@ -905,14 +905,14 @@ def test_last_bang_present
end

def test_last_bang_missing
assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
assert_raises ActiveRecord::RecordNotFound, match: "Couldn't find Topic" do
Topic.where("title = 'This title does not exist'").last!
end
end

def test_model_class_responds_to_last_bang
assert_equal topics(:fifth), Topic.last!
assert_raises_with_message ActiveRecord::RecordNotFound, "Couldn't find Topic" do
assert_raises ActiveRecord::RecordNotFound, match: "Couldn't find Topic" do
Topic.delete_all
Topic.last!
end
Expand Down Expand Up @@ -1390,7 +1390,7 @@ def test_find_by_one_attribute

def test_find_by_one_attribute_bang
assert_equal topics(:first), Topic.find_by_title!("The First Topic")
assert_raises_with_message(ActiveRecord::RecordNotFound, "Couldn't find Topic") do
assert_raises ActiveRecord::RecordNotFound, match: "Couldn't find Topic" do
Topic.find_by_title!("The First Topic!")
end
end
Expand Down Expand Up @@ -1823,9 +1823,4 @@ def self.name
end
end)
end

def assert_raises_with_message(exception_class, message, &block)
err = assert_raises(exception_class) { block.call }
assert_match message, err.message
end
end
7 changes: 1 addition & 6 deletions activesupport/test/core_ext/object/json_gem_encoding_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,11 @@ def assert_same_with_or_without_active_support(subject)
require_or_skip "active_support/core_ext/object/json"

if exception
assert_raises_with_message JSON::GeneratorError, e.message do
assert_raises JSON::GeneratorError, match: e.message do
JSON.generate(subject, quirks_mode: true)
end
else
assert_equal expected, JSON.generate(subject, quirks_mode: true)
end
end

def assert_raises_with_message(exception_class, message, &block)
err = assert_raises(exception_class) { block.call }
assert_match message, err.message
end
end

0 comments on commit 349ed43

Please sign in to comment.