Skip to content

Commit cdcf307

Browse files
committed
Add the actual value to failure messages for have_json_size and have_json_type matchers
1 parent 8f62027 commit cdcf307

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

lib/json_spec/matchers.rb

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,32 +105,36 @@ def scrub(json, path = nil)
105105
include JsonSpec::Helpers
106106

107107
match do |json|
108-
parse_json(json, @path).is_a?(klass)
108+
@json = json
109+
actual.is_a?(klass)
109110
end
110111

111112
chain :at_path do |path|
112113
@path = path
113114
end
114115

115116
failure_message_for_should do
116-
message = "Expected JSON value type to be #{klass}"
117+
message = "Expected JSON value type to be #{klass}, got #{actual.class}"
117118
message << %( at path "#{@path}") if @path
118119
message
119120
end
120121

121122
failure_message_for_should_not do
122-
message = "Expected JSON value type to not be #{klass}"
123+
message = "Expected JSON value type to not be #{klass}, got #{actual.class}"
123124
message << %( at path "#{@path}") if @path
124125
message
125126
end
127+
128+
def actual
129+
parse_json(@json, @path)
130+
end
126131
end
127132

128133
RSpec::Matchers.define :have_json_size do |expected_size|
129134
include JsonSpec::Helpers
130135

131136
match do |json|
132-
ruby = parse_json(json, @path)
133-
actual_size = ruby.is_a?(Enumerable) ? ruby.size : 1
137+
@json = json
134138
actual_size == expected_size
135139
end
136140

@@ -139,14 +143,19 @@ def scrub(json, path = nil)
139143
end
140144

141145
failure_message_for_should do
142-
message = "Expected JSON value size to be #{expected_size}"
146+
message = "Expected JSON value size to be #{expected_size}, got #{actual_size}"
143147
message << %( at path "#{@path}") if @path
144148
message
145149
end
146150

147151
failure_message_for_should_not do
148-
message = "Expected JSON value size to not be #{expected_size}"
152+
message = "Expected JSON value size to not be #{expected_size}, got #{actual_size}"
149153
message << %( at path "#{@path}") if @path
150154
message
151155
end
156+
157+
def actual_size
158+
ruby = parse_json(@json, @path)
159+
ruby.is_a?(Enumerable) ? ruby.size : 1
160+
end
152161
end

spec/json_spec/matchers_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,18 @@
152152
it "matches at a path" do
153153
%({"one":[1,2,3]}).should have_json_size(3).at_path("one")
154154
end
155+
156+
it "provides a failure message for should" do
157+
matcher = have_json_size(3)
158+
matcher.matches?(%([1,2]))
159+
matcher.failure_message_for_should.should == "Expected JSON value size to be 3, got 2"
160+
end
161+
162+
it "provides a failure message for should not" do
163+
matcher = have_json_size(3)
164+
matcher.matches?(%([1,2,3]))
165+
matcher.failure_message_for_should_not.should == "Expected JSON value size to not be 3, got 3"
166+
end
155167
end
156168

157169
context "have_json_path" do
@@ -215,6 +227,18 @@
215227
%(10.0).should have_json_type(Numeric)
216228
end
217229

230+
it "provides a failure message for should" do
231+
matcher = have_json_type(Numeric)
232+
matcher.matches?(%("foo"))
233+
matcher.failure_message_for_should.should == "Expected JSON value type to be Numeric, got String"
234+
end
235+
236+
it "provides a failure message for should not" do
237+
matcher = have_json_type(Numeric)
238+
matcher.matches?(%(10))
239+
matcher.failure_message_for_should_not.should == "Expected JSON value type to not be Numeric, got Fixnum"
240+
end
241+
218242
context "somewhat uselessly" do
219243
it "matches true" do
220244
%(true).should have_json_type(TrueClass)

0 commit comments

Comments
 (0)