Skip to content

Commit d633413

Browse files
sskylarseanpdoyle
authored andcommitted
Fix failure message bug when response is string
- introduce `JsonMatchers::Payload` class to abstract away implementantion details of handling `response` or `String` arguments - include test for failed string response - fix `match_json_schema` typo in README
1 parent 311a3fe commit d633413

File tree

6 files changed

+60
-27
lines changed

6 files changed

+60
-27
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
master
22
======
33

4+
* Fix error message for string responses. [#49]
5+
6+
[#49]: https://github.com/thoughtbot/json_matchers/pull/49
7+
48
0.6.2
59
=====
610

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ describe "GET /posts" do
8181
get posts_path, format: :json
8282

8383
expect(response.status).to eq 200
84-
expect(response.body).to match_json_schema("posts")
84+
expect(response.body).to match_response_schema("posts")
8585
end
8686
end
8787
```

lib/json_matchers/matcher.rb

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require "json-schema"
2+
require "json_matchers/payload"
23

34
module JsonMatchers
45
class Matcher
@@ -10,7 +11,7 @@ def initialize(schema_path, options = {})
1011
def matches?(response)
1112
JSON::Validator.validate!(
1213
schema_path.to_s,
13-
json_from(response).to_s,
14+
Payload.new(response).to_s,
1415
options,
1516
)
1617
rescue JSON::Schema::ValidationError => ex
@@ -28,14 +29,6 @@ def validation_failure_message
2829

2930
attr_reader :schema_path, :options
3031

31-
def json_from(response)
32-
if response.respond_to?(:body)
33-
response.body
34-
else
35-
response
36-
end
37-
end
38-
3932
def default_options
4033
JsonMatchers.configuration.options || {}
4134
end

lib/json_matchers/payload.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module JsonMatchers
2+
class Payload
3+
def initialize(payload)
4+
@payload = extract_json_string(payload)
5+
end
6+
7+
def to_s
8+
payload
9+
end
10+
11+
private
12+
13+
attr_reader :payload
14+
15+
def extract_json_string(payload)
16+
if payload.respond_to?(:body)
17+
payload.body
18+
else
19+
payload
20+
end
21+
end
22+
end
23+
end

lib/json_matchers/rspec.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require "json_matchers"
2+
require "json_matchers/payload"
23

34
module JsonMatchers
45
class RSpec < SimpleDelegator
@@ -18,7 +19,7 @@ def failure_message(response)
1819
1920
expected
2021
21-
#{pretty_json(response.body)}
22+
#{pretty_json(response)}
2223
2324
to match schema "#{schema_name}":
2425
@@ -35,7 +36,7 @@ def failure_message_when_negated(response)
3536
3637
expected
3738
38-
#{pretty_json(response.body)}
39+
#{pretty_json(response)}
3940
4041
not to match schema "#{schema_name}":
4142
@@ -46,8 +47,10 @@ def failure_message_when_negated(response)
4647

4748
private
4849

49-
def pretty_json(json_string)
50-
JSON.pretty_generate(JSON.parse(json_string.to_s))
50+
def pretty_json(response)
51+
payload = Payload.new(response).to_s
52+
53+
JSON.pretty_generate(JSON.parse(payload))
5154
end
5255

5356
def schema_path

spec/json_matchers/match_response_schema_spec.rb

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,30 @@
2222
expect(response_for({})).not_to match_response_schema("foo_schema")
2323
end
2424

25-
it "validates a JSON string" do
26-
create_schema("foo_schema", {
27-
"type" => "object",
28-
"required" => [
29-
"id",
30-
],
31-
"properties" => {
32-
"id" => { "type" => "number" },
33-
},
34-
"additionalProperties" => false,
35-
})
25+
context "when JSON is a string" do
26+
before(:each) do
27+
create_schema("foo_schema", {
28+
"type" => "object",
29+
"required" => [
30+
"id",
31+
],
32+
"properties" => {
33+
"id" => { "type" => "number" },
34+
},
35+
"additionalProperties" => false,
36+
})
37+
end
3638

37-
expect(response_for({ "id" => 1 }).body).
38-
to match_response_schema("foo_schema")
39+
it "validates when the schema matches" do
40+
expect({ "id" => 1 }.to_json).
41+
to match_response_schema("foo_schema")
42+
end
43+
44+
it "fails with message when negated" do
45+
expect {
46+
expect({ "id" => "1" }.to_json).to match_response_schema("foo_schema")
47+
}.to raise_formatted_error(%{{ "type": "number" }})
48+
end
3949
end
4050

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

0 commit comments

Comments
 (0)