Skip to content

Commit

Permalink
Format Failure Message JSON
Browse files Browse the repository at this point in the history
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
  • Loading branch information
seanpdoyle committed Oct 28, 2016
1 parent d0d0b7b commit 2eeb901
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
master
======

* Use `JSON.pretty_generate` to format error messages.

0.6.0
=====

Expand Down
28 changes: 16 additions & 12 deletions lib/json_matchers/rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 10 additions & 4 deletions spec/json_matchers/match_response_schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@

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
create_schema("foo", { "type" => "array" })

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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 2eeb901

Please sign in to comment.