Skip to content

Commit cbacd54

Browse files
Elizabeth LunsfordElizabeth Lunsford
authored andcommitted
do not return 1 entry for nil values
1 parent 50424a8 commit cbacd54

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

features/sizes.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ Feature: Sizes
3535
When I get the JSON
3636
Then the JSON at "one" should have 1 entry
3737
And the JSON at "two" should have 2 values
38-
And the JSON at "three" should have 3 numbers
38+
And the JSON at "three" should have 3 numbers

lib/json_spec/matchers/have_json_size.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ def initialize(size)
1010

1111
def matches?(json)
1212
ruby = parse_json(json, @path)
13-
@actual = Enumerable === ruby ? ruby.size : 1
14-
@actual == @expected
13+
if ruby.nil? && !@expected.nil?
14+
false
15+
else
16+
@actual = Enumerable === ruby ? ruby.size : 1
17+
@actual == @expected
18+
end
1519
end
1620

1721
def at_path(path)

spec/json_spec/matchers/have_json_size_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,16 @@
4646
matcher.matches?(%({"id":1,"json":["spec"]}))
4747
matcher.description.should == %(have JSON size "1" at path "json")
4848
end
49+
50+
it "provides an error when parsing nil" do
51+
matcher = have_json_size(0).at_path("json")
52+
expect { matcher.matches?(%({"id":1,"json":null})) }.to
53+
raise_error(JsonSpec::EnumerableExpected, "Enumerable expected, got nil.")
54+
end
55+
56+
it "provides an error when parsing nil" do
57+
matcher = have_json_size(0).at_path("json")
58+
expect { matcher.matches?(%({"id":1,"json":12345})) }.to
59+
raise_error(JsonSpec::EnumerableExpected, "Enumerable expected, got 12345.")
60+
end
4961
end

0 commit comments

Comments
 (0)