From 2eeb90169664ce54087e57c47ee4b3c8e96841a5 Mon Sep 17 00:00:00 2001 From: Sean Doyle Date: Fri, 28 Oct 2016 13:49:25 -0400 Subject: [PATCH] Format Failure Message JSON This commit modifies the RSpec Matcher to use [`JSON.pretty_generate`][docs] to improve the formatting of JSON bodies and schemata that appear in failure messages. Additionally, the `json-schema` provided error messages have been moved to the top of the failure messages, so that the reasons for failure appear above (potentially lengthy) JSON bodies. [docs]: http://ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html#method-i-pretty_generate --- NEWS.md | 2 ++ lib/json_matchers/rspec.rb | 28 +++++++++++-------- .../match_response_schema_spec.rb | 14 +++++++--- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/NEWS.md b/NEWS.md index a304019..6431144 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,8 @@ master ====== +* Use `JSON.pretty_generate` to format error messages. + 0.6.0 ===== diff --git a/lib/json_matchers/rspec.rb b/lib/json_matchers/rspec.rb index 55cdc58..c0410b2 100644 --- a/lib/json_matchers/rspec.rb +++ b/lib/json_matchers/rspec.rb @@ -12,38 +12,42 @@ def initialize(schema_name, **options) def failure_message(response) <<-FAIL.strip_heredoc - expected + #{validation_failure_message} - #{response.body} + --- - to match schema "#{schema_name}": + expected - #{schema_body} + #{pretty_json(response.body)} - --- + to match schema "#{schema_name}": - #{validation_failure_message} + #{pretty_json(schema_body)} FAIL end def failure_message_when_negated(response) <<-FAIL.strip_heredoc - expected + #{validation_failure_message} - #{response.body} + --- - not to match schema "#{schema_name}": + expected - #{schema_body} + #{pretty_json(response.body)} - --- + not to match schema "#{schema_name}": - #{validation_failure_message} + #{pretty_json(schema_body)} FAIL end + def pretty_json(json_string) + JSON.pretty_generate(JSON.parse(json_string.to_s)) + end + def schema_path JsonMatchers.path_to_schema(schema_name) end diff --git a/spec/json_matchers/match_response_schema_spec.rb b/spec/json_matchers/match_response_schema_spec.rb index ed7c450..1017afa 100644 --- a/spec/json_matchers/match_response_schema_spec.rb +++ b/spec/json_matchers/match_response_schema_spec.rb @@ -72,7 +72,7 @@ expect { expect(response_for("bar" => 5)).to match_response_schema("foo") - }.to raise_error(/{"bar":5}/) + }.to raise_formatted_error(%{{ "bar": 5 }}) end it "contains the body in the failure message when negated" do @@ -80,7 +80,7 @@ expect { expect(response_for([])).not_to match_response_schema("foo") - }.to raise_error(/\[\]/) + }.to raise_formatted_error("[ ]") end it "contains the schema in the failure message" do @@ -89,7 +89,7 @@ expect { expect(response_for("bar" => 5)).to match_response_schema("foo") - }.to raise_error(/#{schema.to_json}/) + }.to raise_formatted_error(%{{ "type": "array" }}) end it "contains the schema in the failure message when negated" do @@ -98,7 +98,7 @@ expect { expect(response_for([])).not_to match_response_schema("foo") - }.to raise_error(/#{schema.to_json}/) + }.to raise_formatted_error(%{{ "type": "array" }}) end it "does not fail when the schema matches" do @@ -129,4 +129,10 @@ expect(valid_response).to match_response_schema("collection") expect(invalid_response).not_to match_response_schema("collection") end + + def raise_formatted_error(error_message) + raise_error { |error| + expect(error.message.squish).to include(error_message) + } + end end