File tree Expand file tree Collapse file tree 6 files changed +60
-27
lines changed Expand file tree Collapse file tree 6 files changed +60
-27
lines changed Original file line number Diff line number Diff line change 11master
22======
33
4+ * Fix error message for string responses. [ #49 ]
5+
6+ [ #49 ] : https://github.com/thoughtbot/json_matchers/pull/49
7+
480.6.2
59=====
610
Original file line number Diff line number Diff 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
8686end
8787```
Original file line number Diff line number Diff line change 11require "json-schema"
2+ require "json_matchers/payload"
23
34module 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
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 11require "json_matchers"
2+ require "json_matchers/payload"
23
34module JsonMatchers
45 class RSpec < SimpleDelegator
@@ -18,7 +19,7 @@ def failure_message(response)
1819
1920expected
2021
21- #{ pretty_json ( response . body ) }
22+ #{ pretty_json ( response ) }
2223
2324to match schema "#{ schema_name } ":
2425
@@ -35,7 +36,7 @@ def failure_message_when_negated(response)
3536
3637expected
3738
38- #{ pretty_json ( response . body ) }
39+ #{ pretty_json ( response ) }
3940
4041not 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
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments