json_matchers 0.6.2 → 0.6.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 68b1658e2e48715aa95963b7dc9f5553ae49e6e4
4
- data.tar.gz: d3536a05e44aaf06757f027d3883e9e9f4af0198
3
+ metadata.gz: aa337859deb80396749bed9a9c4d32d7079a81ab
4
+ data.tar.gz: 3a8dc313f1854e2c1e41cd0bafa66855319bb06e
5
5
  SHA512:
6
- metadata.gz: 1b9eaabfe5e88f41f459762081a3812896080bceda80d96cec2ade4cbfc0242cf3af9819f207116d784c9bc8869d79ed8fd8bcffad276f4317715c3af1b7170d
7
- data.tar.gz: 939c61f64cd34c0f94a0846cf17b18dc61bed92bed78b1adf0f743e74ee6257f8685c1ad31686743f1e531849126dea6ec3a9fa4db985260282c18ae6230ce5f
6
+ metadata.gz: c22878991190e76a633c836dbdc45eb95cc5ce26664c13acf19579940653b4939ea67f212f81c12f2760198716b4347c71803e9135de943e952066cc1f75392f
7
+ data.tar.gz: 706d1bfd640fd52d8743a90cddc3bd2727736167c6dc4deed8137b95b5651bdc880134c43a27a089e367daa76de342aa508ed3ed19c72b4f28949043ab330e57
data/NEWS.md CHANGED
@@ -1,6 +1,13 @@
1
1
  master
2
2
  ======
3
3
 
4
+ 0.6.3
5
+ =====
6
+
7
+ * Fix error message for string responses. [#49]
8
+
9
+ [#49]: https://github.com/thoughtbot/json_matchers/pull/49
10
+
4
11
  0.6.2
5
12
  =====
6
13
 
data/README.md CHANGED
@@ -81,7 +81,7 @@ describe "GET /posts" do
81
81
  get posts_path, format: :json
82
82
 
83
83
  expect(response.status).to eq 200
84
- expect(response.body).to match_json_schema("posts")
84
+ expect(response.body).to match_response_schema("posts")
85
85
  end
86
86
  end
87
87
  ```
@@ -1,4 +1,5 @@
1
1
  require "json-schema"
2
+ require "json_matchers/payload"
2
3
 
3
4
  module JsonMatchers
4
5
  class Matcher
@@ -10,7 +11,7 @@ module JsonMatchers
10
11
  def matches?(response)
11
12
  JSON::Validator.validate!(
12
13
  schema_path.to_s,
13
- json_from(response).to_s,
14
+ Payload.new(response).to_s,
14
15
  options,
15
16
  )
16
17
  rescue JSON::Schema::ValidationError => ex
@@ -28,14 +29,6 @@ module JsonMatchers
28
29
 
29
30
  attr_reader :schema_path, :options
30
31
 
31
- def json_from(response)
32
- if response.respond_to?(:body)
33
- response.body
34
- else
35
- response
36
- end
37
- end
38
-
39
32
  def default_options
40
33
  JsonMatchers.configuration.options || {}
41
34
  end
@@ -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
@@ -1,4 +1,5 @@
1
1
  require "json_matchers"
2
+ require "json_matchers/payload"
2
3
 
3
4
  module JsonMatchers
4
5
  class RSpec < SimpleDelegator
@@ -18,7 +19,7 @@ module JsonMatchers
18
19
 
19
20
  expected
20
21
 
21
- #{pretty_json(response.body)}
22
+ #{pretty_json(response)}
22
23
 
23
24
  to match schema "#{schema_name}":
24
25
 
@@ -35,7 +36,7 @@ to match schema "#{schema_name}":
35
36
 
36
37
  expected
37
38
 
38
- #{pretty_json(response.body)}
39
+ #{pretty_json(response)}
39
40
 
40
41
  not to match schema "#{schema_name}":
41
42
 
@@ -46,8 +47,10 @@ not to match schema "#{schema_name}":
46
47
 
47
48
  private
48
49
 
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))
51
54
  end
52
55
 
53
56
  def schema_path
@@ -1,3 +1,3 @@
1
1
  module JsonMatchers
2
- VERSION = "0.6.2"
2
+ VERSION = "0.6.3"
3
3
  end
@@ -22,20 +22,30 @@ describe JsonMatchers, "#match_response_schema" do
22
22
  expect(response_for({})).not_to match_response_schema("foo_schema")
23
23
  end
24
24
 
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
36
38
 
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
39
49
  end
40
50
 
41
51
  it "fails when the body contains a property with the wrong type" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_matchers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.6.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Doyle
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-28 00:00:00.000000000 Z
11
+ date: 2016-11-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json-schema
@@ -102,6 +102,7 @@ files:
102
102
  - lib/json_matchers/configuration.rb
103
103
  - lib/json_matchers/errors.rb
104
104
  - lib/json_matchers/matcher.rb
105
+ - lib/json_matchers/payload.rb
105
106
  - lib/json_matchers/rspec.rb
106
107
  - lib/json_matchers/version.rb
107
108
  - spec/json_matchers/match_response_schema_spec.rb