module WorkableJsonAssertions::Assertions

Public Instance Methods

assert_json_response_empty() click to toggle source
# File lib/workable_json_assertions/assertions.rb, line 10
def assert_json_response_empty
  assert_empty response.body.strip
end
assert_json_response_equal(json) click to toggle source
# File lib/workable_json_assertions/assertions.rb, line 14
def assert_json_response_equal(json)
  json = json.with_indifferent_access if json.is_a?(Hash)
  assert_equal json, json_response_body
end
assert_json_response_equal_except(json, blacklist = []) click to toggle source
# File lib/workable_json_assertions/assertions.rb, line 19
def assert_json_response_equal_except(json, blacklist = [])
  json = json.with_indifferent_access if json.is_a?(Hash)
  assert_json_equal_except(json, json_response_body, blacklist)
end
assert_json_response_has_key(key) click to toggle source
# File lib/workable_json_assertions/assertions.rb, line 28
def assert_json_response_has_key(key)
  assert json_response_body.has_key?(key.to_sym)
end
assert_json_response_includes(hash) click to toggle source
# File lib/workable_json_assertions/assertions.rb, line 24
def assert_json_response_includes(hash)
  assert_match hash, json_response_body
end
json_response_body(options = {symbolize_names: true}) click to toggle source
# File lib/workable_json_assertions/assertions.rb, line 6
def json_response_body(options = {symbolize_names: true})
  JSON.parse(response.body.strip, options)
end
refute_json_response_has_key(key) click to toggle source
# File lib/workable_json_assertions/assertions.rb, line 32
def refute_json_response_has_key(key)
  refute json_response_body.has_key?(key.to_sym)
end

Private Instance Methods

assert_json_equal_except(json1, json2, blacklist = []) click to toggle source
# File lib/workable_json_assertions/assertions.rb, line 38
def assert_json_equal_except(json1, json2, blacklist = [])
  json1 = recursive_except(json1, blacklist)
  json2 = recursive_except(json2, blacklist)
  assert_equal json1, json2
end
recursive_except(obj, blacklist) click to toggle source
# File lib/workable_json_assertions/assertions.rb, line 44
def recursive_except(obj, blacklist)
  case obj
  when Hash
    obj = obj.with_indifferent_access
    obj.each do |k, v|
      obj[k] = recursive_except(v, blacklist)
    end.except(*blacklist)
  when Array
    obj.map do |i|
      recursive_except(i, blacklist)
    end
  else
    obj
  end
end