Skip to content

Commit a5fd8d9

Browse files
committed
Merge pull request #53 from DanOlson/master
Provide better failure messages for include_json matcher. Addresses issu...
2 parents 50424a8 + 2a3111d commit a5fd8d9

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lib/json_spec/matchers/include_json.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ def initialize(expected_json = nil)
1212
def matches?(actual_json)
1313
raise "Expected included JSON not provided" if @expected_json.nil?
1414

15+
@actual_json = actual_json
16+
1517
actual = parse_json(actual_json, @path)
1618
expected = exclude_keys(parse_json(@expected_json))
1719
case actual
@@ -43,12 +45,12 @@ def including(*keys)
4345
end
4446

4547
def failure_message
46-
message_with_path("Expected included JSON")
48+
message_with_path("Expected #{@actual_json} to include #{@expected_json}")
4749
end
4850
alias :failure_message_for_should :failure_message
4951

5052
def failure_message_when_negated
51-
message_with_path("Expected excluded JSON")
53+
message_with_path("Expected #{@actual_json} to not include #{@expected_json}")
5254
end
5355
alias :failure_message_for_should_not :failure_message_when_negated
5456

spec/json_spec/matchers/include_json_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@
7171
matcher.description.should == %(include JSON at path "json/0")
7272
end
7373

74+
it "provides a useful failure message for should" do
75+
matcher = include_json(%([4,5,6]))
76+
matcher.matches?(%([1,2,3]))
77+
matcher.failure_message_for_should.should == "Expected [1,2,3] to include [4,5,6]"
78+
end
79+
80+
it "provides a useful failure message for should not" do
81+
matcher = include_json(%(3))
82+
matcher.matches?(%([1,2,3]))
83+
matcher.failure_message_for_should_not.should == "Expected [1,2,3] to not include 3"
84+
end
85+
7486
it "raises an error when not given expected JSON" do
7587
expect{ %([{"id":1,"two":3}]).should include_json }.to raise_error
7688
end

0 commit comments

Comments
 (0)