File tree Expand file tree Collapse file tree 2 files changed +28
-2
lines changed Expand file tree Collapse file tree 2 files changed +28
-2
lines changed Original file line number Diff line number Diff line change @@ -5,13 +5,17 @@ class IncludeJson
5
5
include JsonSpec ::Exclusion
6
6
include JsonSpec ::Messages
7
7
8
+ attr_reader :actual_json
9
+
8
10
def initialize ( expected_json = nil )
9
11
@expected_json = expected_json
10
12
end
11
13
12
14
def matches? ( actual_json )
13
15
raise "Expected included JSON not provided" if @expected_json . nil?
14
16
17
+ self . actual_json = actual_json
18
+
15
19
actual = parse_json ( actual_json , @path )
16
20
expected = exclude_keys ( parse_json ( @expected_json ) )
17
21
case actual
@@ -43,18 +47,24 @@ def including(*keys)
43
47
end
44
48
45
49
def failure_message
46
- message_with_path ( "Expected included JSON " )
50
+ message_with_path ( "Expected #{ actual_json } to include #{ @expected_json } " )
47
51
end
48
52
alias :failure_message_for_should :failure_message
49
53
50
54
def failure_message_when_negated
51
- message_with_path ( "Expected excluded JSON " )
55
+ message_with_path ( "Expected #{ actual_json } to exclude #{ @expected_json } " )
52
56
end
53
57
alias :failure_message_for_should_not :failure_message_when_negated
54
58
55
59
def description
56
60
message_with_path ( "include JSON" )
57
61
end
62
+
63
+ private
64
+
65
+ def actual_json = ( json )
66
+ @actual_json = json
67
+ end
58
68
end
59
69
end
60
70
end
Original file line number Diff line number Diff line change 71
71
matcher . description . should == %(include JSON at path "json/0")
72
72
end
73
73
74
+ it "provides a useful failure message for should" do
75
+ actual = %({"ids": [1,2,3]})
76
+ expected = %({"ids": [4,5,6]})
77
+ matcher = include_json ( expected )
78
+ matcher . matches? ( actual )
79
+ matcher . failure_message_for_should . should == "Expected #{ actual } to include #{ expected } "
80
+ end
81
+
82
+ it "provides a useful failure message for should not" do
83
+ actual = %({"ids": [1,2,3]})
84
+ expected = actual
85
+ matcher = include_json ( expected )
86
+ matcher . matches? ( actual )
87
+ matcher . failure_message_for_should_not . should == "Expected #{ actual } to exclude #{ expected } "
88
+ end
89
+
74
90
it "raises an error when not given expected JSON" do
75
91
expect { %([{"id":1,"two":3}]) . should include_json } . to raise_error
76
92
end
You can’t perform that action at this time.
0 commit comments