Sha256: da7b0e510728a8d962eaf4210c4a4b400c059e3c55a1f9aa51c9ed8bbafddd58

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

require 'active_support/core_ext/hash/indifferent_access'

module WorkableJsonAssertions
  module Assertions

    def assert_json_response_empty
      assert_empty response.body.strip
    end

    def assert_json_response_equal(json)
      json = json.with_indifferent_access if json.is_a?(Hash)
      assert_equal json, JSON.parse(response.body.strip)
    end

    def assert_json_response_equal_except(json, blacklist = [])
      json = json.with_indifferent_access if json.is_a?(Hash)
      assert_json_equal_except(json, JSON.parse(response.body), blacklist)
    end

    def assert_json_response_includes(hash)
      assert_match hash.to_json, response.body
    end

    private

    def assert_json_equal_except(json1, json2, blacklist = [])
      json1 = recursive_except(json1, blacklist)
      json2 = recursive_except(json2, blacklist)
      assert_equal json1, json2
    end

    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
workable_json_assertions-0.1.1 lib/workable_json_assertions/assertions.rb