diff options
5 files changed, 31 insertions, 3 deletions
diff --git a/activestorage/lib/active_storage/attached/changes/create_one.rb b/activestorage/lib/active_storage/attached/changes/create_one.rb index bb59a651ba..98aea36861 100644 --- a/activestorage/lib/active_storage/attached/changes/create_one.rb +++ b/activestorage/lib/active_storage/attached/changes/create_one.rb @@ -58,7 +58,7 @@ module ActiveStorage when String ActiveStorage::Blob.find_signed(attachable) else - raise "Could not find or build blob: expected attachable, got #{attachable.inspect}" + raise ArgumentError, "Could not find or build blob: expected attachable, got #{attachable.inspect}" end end end diff --git a/activestorage/test/models/attached/many_test.rb b/activestorage/test/models/attached/many_test.rb index ff2d29de11..bc44e9da68 100644 --- a/activestorage/test/models/attached/many_test.rb +++ b/activestorage/test/models/attached/many_test.rb @@ -253,7 +253,7 @@ class ActiveStorage::ManyAttachedTest < ActiveSupport::TestCase end test "creating a record with an unexpected object attached" do - error = assert_raises { User.create!(name: "Jason", highlights: :foo) } + error = assert_raises(ArgumentError) { User.create!(name: "Jason", highlights: :foo) } assert_equal "Could not find or build blob: expected attachable, got :foo", error.message end diff --git a/activestorage/test/models/attached/one_test.rb b/activestorage/test/models/attached/one_test.rb index af45b696ae..8654ecffef 100644 --- a/activestorage/test/models/attached/one_test.rb +++ b/activestorage/test/models/attached/one_test.rb @@ -255,7 +255,7 @@ class ActiveStorage::OneAttachedTest < ActiveSupport::TestCase end test "creating a record with an unexpected object attached" do - error = assert_raises { User.create!(name: "Jason", avatar: :foo) } + error = assert_raises(ArgumentError) { User.create!(name: "Jason", avatar: :foo) } assert_equal "Could not find or build blob: expected attachable, got :foo", error.message end diff --git a/activesupport/lib/active_support/testing/assertions.rb b/activesupport/lib/active_support/testing/assertions.rb index 6a56da384f..b27ac7ce99 100644 --- a/activesupport/lib/active_support/testing/assertions.rb +++ b/activesupport/lib/active_support/testing/assertions.rb @@ -113,11 +113,23 @@ module ActiveSupport # post :create, params: { article: invalid_attributes } # end # + # A lambda can be passed in and evaluated. + # + # assert_no_difference -> { Article.count } do + # post :create, params: { article: invalid_attributes } + # end + # # An error message can be specified. # # assert_no_difference 'Article.count', 'An Article should not be created' do # post :create, params: { article: invalid_attributes } # end + # + # An array of expressions can also be passed in and evaluated. + # + # assert_no_difference [ 'Article.count', -> { Post.count } ] do + # post :create, params: { article: invalid_attributes } + # end def assert_no_difference(expression, message = nil, &block) assert_difference expression, 0, message, &block end diff --git a/activesupport/test/test_case_test.rb b/activesupport/test/test_case_test.rb index 19901fad99..8698c66e6d 100644 --- a/activesupport/test/test_case_test.rb +++ b/activesupport/test/test_case_test.rb @@ -52,6 +52,22 @@ class AssertionsTest < ActiveSupport::TestCase assert_equal "Object Changed.\n\"@object.num\" didn't change by 0.\nExpected: 0\n Actual: 1", error.message end + def test_assert_no_difference_with_multiple_expressions_pass + another_object = @object.dup + assert_no_difference ["@object.num", -> { another_object.num }] do + # ... + end + end + + def test_assert_no_difference_with_multiple_expressions_fail + another_object = @object.dup + assert_raises(Minitest::Assertion) do + assert_no_difference ["@object.num", -> { another_object.num }], "Another Object Changed" do + another_object.increment + end + end + end + def test_assert_difference assert_difference "@object.num", +1 do @object.increment |