json-matchers 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/NEWS.md +5 -0
- data/lib/json/matchers/rspec.rb +56 -3
- data/lib/json/matchers/version.rb +1 -1
- data/spec/json/matchers/match_response_schema_spec.rb +38 -4
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5e513acd2c16118fcb7e208b22f926418418b375
|
4
|
+
data.tar.gz: 5bda78905776236ae7626748b486bbd0e6bf5f30
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 74fcf59b8e5dcd2e45c5254631da38c81961eff1aafd79d3918e05c7982211c6a3f618437bd7c01f4d219d7f7b7f09792365201e2cf2e7a3cd65032585b2299b
|
7
|
+
data.tar.gz: 6c98bbf6bbd6d1bb7af762a7b3905de94636062639a201309778c168e63bf661a0d1d856fe9d92ff312cce80f2f34f6e8eb13f69d1e9e164aca93b5a6724c99c
|
data/NEWS.md
CHANGED
data/lib/json/matchers/rspec.rb
CHANGED
@@ -1,10 +1,63 @@
|
|
1
|
+
module JSON
|
2
|
+
module Matchers
|
3
|
+
class RSpec < SimpleDelegator
|
4
|
+
attr_reader :schema_name
|
5
|
+
|
6
|
+
def initialize(schema_name)
|
7
|
+
@schema_name = schema_name
|
8
|
+
|
9
|
+
super(JSON::Matchers::Matcher.new(schema_path))
|
10
|
+
end
|
11
|
+
|
12
|
+
def failure_message(response)
|
13
|
+
<<-FAIL.strip_heredoc
|
14
|
+
expected
|
15
|
+
|
16
|
+
#{response.body}
|
17
|
+
|
18
|
+
to match schema "#{schema_name}":
|
19
|
+
|
20
|
+
#{schema_body}
|
21
|
+
FAIL
|
22
|
+
end
|
23
|
+
|
24
|
+
def failure_message_when_negated(response)
|
25
|
+
<<-FAIL.strip_heredoc
|
26
|
+
expected
|
27
|
+
|
28
|
+
#{response.body}
|
29
|
+
|
30
|
+
not to match schema "#{schema_name}":
|
31
|
+
|
32
|
+
#{schema_body}
|
33
|
+
FAIL
|
34
|
+
end
|
35
|
+
|
36
|
+
def schema_path
|
37
|
+
JSON::Matchers.path_to_schema(schema_name)
|
38
|
+
end
|
39
|
+
|
40
|
+
def schema_body
|
41
|
+
File.read(schema_path)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
1
47
|
if RSpec.respond_to?(:configure)
|
2
48
|
RSpec::Matchers.define :match_response_schema do |schema_name|
|
3
|
-
|
4
|
-
schema_path = JSON::Matchers.path_to_schema(schema_name)
|
5
|
-
matcher = JSON::Matchers::Matcher.new(schema_path)
|
49
|
+
matcher = JSON::Matchers::RSpec.new(schema_name)
|
6
50
|
|
51
|
+
match do |response|
|
7
52
|
matcher.matches?(response)
|
8
53
|
end
|
54
|
+
|
55
|
+
failure_message do |response|
|
56
|
+
matcher.failure_message(response)
|
57
|
+
end
|
58
|
+
|
59
|
+
failure_message_when_negated do |response|
|
60
|
+
matcher.failure_message_when_negated(response)
|
61
|
+
end
|
9
62
|
end
|
10
63
|
end
|
@@ -14,23 +14,57 @@ describe JSON::Matchers, "#match_response_schema" do
|
|
14
14
|
end
|
15
15
|
|
16
16
|
it "fails when the body is missing a required property" do
|
17
|
-
create_schema("
|
17
|
+
create_schema("foo_schema", {
|
18
18
|
type: "object",
|
19
19
|
required: ["foo"],
|
20
20
|
})
|
21
21
|
|
22
|
-
expect(response_for({})).not_to match_response_schema("
|
22
|
+
expect(response_for({})).not_to match_response_schema("foo_schema")
|
23
23
|
end
|
24
24
|
|
25
25
|
it "fails when the body contains a property with the wrong type" do
|
26
|
-
create_schema("
|
26
|
+
create_schema("foo_schema", {
|
27
27
|
type: "object",
|
28
28
|
properties: {
|
29
29
|
foo: { type: "string" }
|
30
30
|
}
|
31
31
|
})
|
32
32
|
|
33
|
-
expect(response_for({foo: 1})).not_to match_response_schema("
|
33
|
+
expect(response_for({foo: 1})).not_to match_response_schema("foo_schema")
|
34
|
+
end
|
35
|
+
|
36
|
+
it "contains the body in the failure message" do
|
37
|
+
create_schema("foo", { type: "array" })
|
38
|
+
|
39
|
+
expect {
|
40
|
+
expect(response_for(bar: 5)).to match_response_schema("foo")
|
41
|
+
}.to raise_error(/{"bar":5}/)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "contains the body in the failure message when negated" do
|
45
|
+
create_schema("foo", { type: "array" })
|
46
|
+
|
47
|
+
expect {
|
48
|
+
expect(response_for([])).not_to match_response_schema("foo")
|
49
|
+
}.to raise_error(/\[\]/)
|
50
|
+
end
|
51
|
+
|
52
|
+
it "contains the schema in the failure message" do
|
53
|
+
schema = { type: "array" }
|
54
|
+
create_schema("foo", schema)
|
55
|
+
|
56
|
+
expect {
|
57
|
+
expect(response_for(bar: 5)).to match_response_schema("foo")
|
58
|
+
}.to raise_error(/#{schema.to_json}/)
|
59
|
+
end
|
60
|
+
|
61
|
+
it "contains the schema in the failure message when negated" do
|
62
|
+
schema = { type: "array" }
|
63
|
+
create_schema("foo", schema)
|
64
|
+
|
65
|
+
expect {
|
66
|
+
expect(response_for([])).not_to match_response_schema("foo")
|
67
|
+
}.to raise_error(/#{schema.to_json}/)
|
34
68
|
end
|
35
69
|
|
36
70
|
it "does not fail when the schema matches" 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.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sean Doyle
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-10-
|
11
|
+
date: 2014-10-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json-schema
|
@@ -138,7 +138,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
138
138
|
version: '0'
|
139
139
|
requirements: []
|
140
140
|
rubyforge_project:
|
141
|
-
rubygems_version: 2.4.
|
141
|
+
rubygems_version: 2.4.2
|
142
142
|
signing_key:
|
143
143
|
specification_version: 4
|
144
144
|
summary: Validate your Rails JSON API's JSON
|