Sha256: 76e9d028805c21974203100dbdf4e2e11413c97aa378ffab9a0c851df9810a6e

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

require 'spec_helper'

describe Grape::Validations::MultipleAttributesIterator do
  describe '#each' do
    subject(:iterator) { described_class.new(validator, scope, params) }

    let(:scope) { Grape::Validations::ParamsScope.new(api: Class.new(Grape::API)) }
    let(:validator) { double(attrs: %i[first second third]) }

    context 'when params is a hash' do
      let(:params) do
        { first: 'string', second: 'string' }
      end

      it 'yields the whole params hash and the skipped flag without the list of attrs' do
        expect { |b| iterator.each(&b) }.to yield_with_args(params, false)
      end
    end

    context 'when params is an array' do
      let(:params) do
        [{ first: 'string1', second: 'string1' }, { first: 'string2', second: 'string2' }]
      end

      it 'yields each element of the array without the list of attrs' do
        expect { |b| iterator.each(&b) }.to yield_successive_args([params[0], false], [params[1], false])
      end
    end

    context 'when params is empty optional placeholder' do
      let(:params) do
        [Grape::DSL::Parameters::EmptyOptionalValue, { first: 'string2', second: 'string2' }]
      end

      it 'yields each element of the array without the list of attrs' do
        expect { |b| iterator.each(&b) }.to yield_successive_args([Grape::DSL::Parameters::EmptyOptionalValue, true], [params[1], false])
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
grape-1.6.2 spec/grape/validations/multiple_attributes_iterator_spec.rb
grape-1.6.1 spec/grape/validations/multiple_attributes_iterator_spec.rb