# frozen_string_literal: true require 'active_support/hash_with_indifferent_access' require 'rspec/rails/api/utils' ## # RSpec matcher to check something against an array of `expected` # # 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? # Primitive type if expected[:type].is_a?(Symbol) @actual.each do |item| return false unless RSpec::Rails::Api::Utils.check_value_type(expected[:type], item) end return true end # Check every entry @actual.each do |item| return false unless RSpec::Rails::Api::Utils.validate_object_structure item, expected end true end diffable end ## # RSpec matcher to check something against the `expected` definition # 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