Class: JsonAssertions::JsonResponseTester

Inherits:
Object
  • Object
show all
Defined in:
lib/json_assertions.rb

Instance Method Summary collapse

Constructor Details

#initialize(json, controller_test) ⇒ JsonResponseTester

Returns a new instance of JsonResponseTester.



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/json_assertions.rb', line 48

def initialize(json, controller_test)
  @controller_test = controller_test
  if json.class == String
    begin
      @json_hash = JSON.parse json
    rescue Exception
      test_failure "Could not parse JSON string \"#{json.slice(0, 100)}\""
    end
  else
    @json_hash = json
  end
end

Instance Method Details

#[](key) ⇒ Object



61
62
63
# File 'lib/json_assertions.rb', line 61

def [](key)
  return @json_hash[key]
end

#array_map_equals(expected_list, list_path, value_path, message = nil) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/json_assertions.rb', line 151

def array_map_equals(expected_list, list_path, value_path, message=nil)
  value_list = value_at(list_path).map do |hash|

    last = hash

    value_path.split('/').each do |index|
      next if index == ''

      if index =~ /\d/
        last = last[index.to_i]
      else
        last = last[index]
      end

    end
    last
  end

  if expected_list != value_list
    error_msg = message || "List did not equal #{expected_list.inspect} #{value_list.inspect}"
    test_failure error_msg
  else
    @controller_test.assert true
  end
end

#array_size_gte(expected_size, path, message = nil) ⇒ Object



117
118
119
120
121
122
123
124
125
126
# File 'lib/json_assertions.rb', line 117

def array_size_gte(expected_size, path, message=nil)
  is_array?(path)
  value = value_at(path)
  unless value.size >= expected_size
    error_msg = message || "JSON array at #{path} expected to be gte #{expected_size} elements but has #{value.size}."
    test_failure error_msg, path
  else
    @controller_test.assert true
  end
end

#array_size_is(expected_size, path, message = nil) ⇒ Object Also known as: array_length_is



129
130
131
132
133
134
135
136
137
138
# File 'lib/json_assertions.rb', line 129

def array_size_is(expected_size, path, message=nil)
  is_array?(path)
  value = value_at(path)
  unless value.size == expected_size
    error_msg = message || "JSON array at #{path} expected have #{expected_size} elements but has #{value.size}."
    test_failure error_msg, path
  else
    @controller_test.assert true
  end
end

#equal(expected, path, message = nil) ⇒ Object Also known as: equals

equal

Parameters:



69
70
71
72
73
74
75
76
77
78
# File 'lib/json_assertions.rb', line 69

def equal(expected, path, message=nil)
  value = value_at(path)
  
  if value != expected
    error_msg = message || "JSON value #{value} != #{expected} at path #{path}"
    test_failure error_msg, path
  else
    @controller_test.assert true
  end
end

#is_array?(path, message = nil) ⇒ Boolean Also known as: is_array

Returns:



141
142
143
144
145
146
147
148
# File 'lib/json_assertions.rb', line 141

def is_array?(path, message=nil)
  if value_at(path).class != Array
    error_msg = message || "JSON at path #{path} expected to be an array."
    test_failure error_msg, path
  else
    @controller_test.assert true
  end
end

#matches(regex, path, message = nil) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/json_assertions.rb', line 81

def matches(regex, path, message=nil)
  unless regex =~ value_at(path)
    error_msg = message || "JSON value of #{value_at(path).inspect} at path #{path} does not match #{regex}."
    test_failure error_msg, path
  else
    @controller_test.assert true
  end
  true
end

#must_have(path, message = nil) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/json_assertions.rb', line 91

def must_have(path, message=nil)
  error_msg = message || "JSON path #{path} does not exist but is expected."
  begin
    value = value_at(path)
    if value.nil?
      test_failure error_msg, path
    else
      @controller_test.assert true
    end
  rescue Exception
    test_failure error_msg, path
  end
  true
end

#must_not_have(path, message = nil) ⇒ Object



106
107
108
109
110
111
112
113
114
115
# File 'lib/json_assertions.rb', line 106

def must_not_have(path, message=nil)
  value = value_at(path)
  if value != nil
    error_msg = message || "JSON path #{path} exists but expected to be missing."
    test_failure error_msg, path
  else
    @controller_test.assert true
  end
  return true
end

#value_at(path, message = nil) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/json_assertions.rb', line 178

def value_at(path, message=nil)
  if path[0] != '/'
    error_msg = message || "Currently we can only scan JSON from base. Start path with /"
    test_failure error_msg
  end

  last = @json_hash

  path.split('/').each do |index|
    next if index == ''

    if index =~ /\d/
      #puts last.inspect
      #puts index.to_i
      last = last[index.to_i]
    else
      last = last[index]
    end

  end

  return last
rescue TypeError
  test_failure "JSON path #{path} is not the type expected. Array instead of object perhaps."
rescue NoMethodError
  test_failure "JSON path #{path} does not exist.", path
end