From d0d7af470c2f428e3845f4310ad16715a8b84221 Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Wed, 20 Apr 2016 09:13:27 -0500 Subject: [PATCH 1/2] Test::Schema exceptions should be Minitest::Assertions --- lib/active_model_serializers/test/schema.rb | 4 ++-- test/active_model_serializers/test/schema_test.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/active_model_serializers/test/schema.rb b/lib/active_model_serializers/test/schema.rb index 7674f6be..ee2adc3d 100644 --- a/lib/active_model_serializers/test/schema.rb +++ b/lib/active_model_serializers/test/schema.rb @@ -14,8 +14,8 @@ module ActiveModelSerializers assert(matcher.call, matcher.message) end - MissingSchema = Class.new(Errno::ENOENT) - InvalidSchemaError = Class.new(StandardError) + MissingSchema = Class.new(Minitest::Assertion) + InvalidSchemaError = Class.new(Minitest::Assertion) class AssertResponseSchema attr_reader :schema_path, :response, :message diff --git a/test/active_model_serializers/test/schema_test.rb b/test/active_model_serializers/test/schema_test.rb index 16128437..7e1b5d48 100644 --- a/test/active_model_serializers/test/schema_test.rb +++ b/test/active_model_serializers/test/schema_test.rb @@ -102,14 +102,14 @@ module ActiveModelSerializers end def test_with_a_non_existent_file - message = %r{.* - No Schema file at test/support/schemas/non-existent.json} + expected_message = 'No Schema file at test/support/schemas/non-existent.json' get :show error = assert_raises ActiveModelSerializers::Test::Schema::MissingSchema do assert_response_schema('non-existent.json') end - assert_match(message, error.message) + assert_equal(expected_message, error.message) end def test_that_raises_with_a_invalid_json_body From 93cad825b78e04dca1808a095496a83231c620ed Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Wed, 20 Apr 2016 10:09:49 -0500 Subject: [PATCH 2/2] Include actual exception message with custom exceptions --- CHANGELOG.md | 2 ++ lib/active_model_serializers/test/schema.rb | 2 +- test/active_model_serializers/test/schema_test.rb | 8 +++++--- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ec9b7446..5488ad17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ Breaking changes: - [#1662](https://github.com/rails-api/active_model_serializers/pull/1662) Drop support for Rails 4.0 and Ruby 2.0.0. (@remear) Features: +- [#1697](https://github.com/rails-api/active_model_serializers/pull/1697) Include actual exception message with custom exceptions; + `Test::Schema` exceptions are now `Minitest::Assertion`s. (@bf4) - [#1699](https://github.com/rails-api/active_model_serializers/pull/1699) String/Lambda support for conditional attributes/associations (@mtsmfm) - [#1687](https://github.com/rails-api/active_model_serializers/pull/1687) Only calculate `_cache_digest` (in `cache_key`) when `skip_digest` is false. (@bf4) - [#1647](https://github.com/rails-api/active_model_serializers/pull/1647) Restrict usage of `serializable_hash` options diff --git a/lib/active_model_serializers/test/schema.rb b/lib/active_model_serializers/test/schema.rb index ee2adc3d..b356608e 100644 --- a/lib/active_model_serializers/test/schema.rb +++ b/lib/active_model_serializers/test/schema.rb @@ -32,7 +32,7 @@ module ActiveModelSerializers def call json_schema.expand_references!(store: document_store) status, errors = json_schema.validate(response_body) - @message ||= errors.map(&:to_s).to_sentence + @message = [message, errors.map(&:to_s).to_sentence].compact.join(': ') status end diff --git a/test/active_model_serializers/test/schema_test.rb b/test/active_model_serializers/test/schema_test.rb index 7e1b5d48..105ac575 100644 --- a/test/active_model_serializers/test/schema_test.rb +++ b/test/active_model_serializers/test/schema_test.rb @@ -54,13 +54,15 @@ module ActiveModelSerializers def test_that_raises_error_with_a_custom_message_with_a_invalid_schema message = 'oh boy the show is broken' + exception_message = "#/name: failed schema #/properties/name: For 'properties/name', \"Name 1\" is not an integer. and #/description: failed schema #/properties/description: For 'properties/description', \"Description 1\" is not a boolean." + expected_message = "#{message}: #{exception_message}" get :show error = assert_raises Minitest::Assertion do assert_response_schema(nil, message) end - assert_equal(message, error.message) + assert_equal(expected_message, error.message) end def test_that_assert_with_a_custom_schema @@ -102,14 +104,14 @@ module ActiveModelSerializers end def test_with_a_non_existent_file - expected_message = 'No Schema file at test/support/schemas/non-existent.json' + message = 'No Schema file at test/support/schemas/non-existent.json' get :show error = assert_raises ActiveModelSerializers::Test::Schema::MissingSchema do assert_response_schema('non-existent.json') end - assert_equal(expected_message, error.message) + assert_equal(message, error.message) end def test_that_raises_with_a_invalid_json_body