Sha256: 056edceff28a6d72fa23978a97b802225dfda0268549b249fb91ec106ae35622

Contents?: true

Size: 1.06 KB

Versions: 13

Compression:

Stored size: 1.06 KB

Contents

# 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

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
rspec-rails-api-0.3.4 lib/rspec/rails/api/matchers.rb
rspec-rails-api-0.3.3 lib/rspec/rails/api/matchers.rb
rspec-rails-api-0.3.2 lib/rspec/rails/api/matchers.rb
rspec-rails-api-0.3.1 lib/rspec/rails/api/matchers.rb
rspec-rails-api-0.3.0 lib/rspec/rails/api/matchers.rb
rspec-rails-api-0.2.3 lib/rspec/rails/api/matchers.rb
rspec-rails-api-0.2.2 lib/rspec/rails/api/matchers.rb
rspec-rails-api-0.2.0 lib/rspec/rails/api/matchers.rb
rspec-rails-api-0.1.5 lib/rspec/rails/api/matchers.rb
rspec-rails-api-0.1.4 lib/rspec/rails/api/matchers.rb
rspec-rails-api-0.1.3 lib/rspec/rails/api/matchers.rb
rspec-rails-api-0.1.2 lib/rspec/rails/api/matchers.rb
rspec-rails-api-0.1.0 lib/rspec/rails/api/matchers.rb