# frozen_string_literal: true require 'active_support/hash_with_indifferent_access' require 'rspec/rails/api/utils' # FIXME: Split the matcher in something else; it's too messy. RSpec::Matchers.define :have_many do |expected| match do |actual| @actual = actual @actual = JSON.parse(actual.body) if actual.respond_to? :body raise "Response is not an array: #{@actual.class}" unless @actual.is_a? Array raise 'Response has no item to compare with' unless @actual.count.positive? # Check every entry ok = true @actual.each do |item| ok = false unless RSpec::Rails::Api::Utils.validate_object_structure item, expected end ok end diffable end # FIXME: Split the matcher in something else; it's too messy. RSpec::Matchers.define :have_one do |expected| match do |actual| @actual = actual @actual = JSON.parse(actual.body) if actual.respond_to? :body raise "Response is not a hash: #{@actual.class}" unless @actual.is_a? Hash RSpec::Rails::Api::Utils.validate_object_structure @actual, expected end diffable end