Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error messages for string responses #49

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe "GET /posts" do
get posts_path, format: :json

expect(response.status).to eq 200
expect(response.body).to match_json_schema("posts")
expect(response.body).to match_response_schema("posts")
end
end
```
Expand Down
7 changes: 4 additions & 3 deletions lib/json_matchers/rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def failure_message(response)

expected

#{pretty_json(response.body)}
#{pretty_json(response)}

to match schema "#{schema_name}":

Expand All @@ -35,7 +35,7 @@ def failure_message_when_negated(response)

expected

#{pretty_json(response.body)}
#{pretty_json(response)}

not to match schema "#{schema_name}":

Expand All @@ -46,7 +46,8 @@ def failure_message_when_negated(response)

private

def pretty_json(json_string)
def pretty_json(response)
json_string = response.respond_to?(:body) ? response.body : response
JSON.pretty_generate(JSON.parse(json_string.to_s))
end

Expand Down
36 changes: 23 additions & 13 deletions spec/json_matchers/match_response_schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,30 @@
expect(response_for({})).not_to match_response_schema("foo_schema")
end

it "validates a JSON string" do
create_schema("foo_schema", {
"type" => "object",
"required" => [
"id",
],
"properties" => {
"id" => { "type" => "number" },
},
"additionalProperties" => false,
})
context "when JSON is a string" do
before(:each) do
create_schema("foo_schema", {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant curly braces around a hash parameter.

"type" => "object",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use 2 spaces for indentation in a hash, relative to the first position after the preceding left parenthesis.

"required" => [
"id",
],
"properties" => {
"id" => { "type" => "number" },
},
"additionalProperties" => false,
})

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indent the right brace the same as the first position after the preceding left parenthesis.

end

expect(response_for({ "id" => 1 }).body).
to match_response_schema("foo_schema")
it "validates when the schema matches" do
expect({ "id" => 1 }.to_json).
to match_response_schema("foo_schema")
end

it "fails with message when negated" do
expect {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid using {...} for multi-line blocks.

expect({ "id" => "1" }.to_json).to match_response_schema("foo_schema")
}.to raise_formatted_error(%{{ "type": "number" }})
end
end

it "fails when the body contains a property with the wrong type" do
Expand Down