Sha256: bbbbbdd98313159d18474010ecaa9e4568be9ddae048d6f2366501ad9207e85f

Contents?: true

Size: 1.88 KB

Versions: 1

Compression:

Stored size: 1.88 KB

Contents

require 'spec_helper'

describe Grape::Validations::AtLeastOneOfValidator do
  describe '#validate!' do
    let(:scope) do
      Struct.new(:opts) do
        def params(arg); end
      end
    end
    let(:at_least_one_of_params) { [:beer, :wine, :grapefruit] }
    let(:validator) { described_class.new(at_least_one_of_params, {}, false, scope.new) }

    context 'when all restricted params are present' do
      let(:params) { { beer: true, wine: true, grapefruit: true } }

      it 'does not raise a validation exception' do
        expect(validator.validate!(params)).to eql params
      end

      context 'mixed with other params' do
        let(:mixed_params) { params.merge!(other: true, andanother: true) }

        it 'does not raise a validation exception' do
          expect(validator.validate!(mixed_params)).to eql mixed_params
        end
      end
    end

    context 'when a subset of restricted params are present' do
      let(:params) { { beer: true, grapefruit: true } }

      it 'does not raise a validation exception' do
        expect(validator.validate!(params)).to eql params
      end
    end

    context 'when params keys come as strings' do
      let(:params) { { 'beer' => true, 'grapefruit' => true } }

      it 'does not raise a validation exception' do
        expect(validator.validate!(params)).to eql params
      end
    end

    context 'when none of the restricted params is selected' do
      let(:params) { { somethingelse: true } }

      it 'raises a validation exception' do
        expect {
          validator.validate! params
        }.to raise_error(Grape::Exceptions::Validation)
      end
    end

    context 'when exactly one of the restricted params is selected' do
      let(:params) { { beer: true, somethingelse: true } }

      it 'does not raise a validation exception' do
        expect(validator.validate!(params)).to eql params
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
grape-0.9.0 spec/grape/validations/at_least_one_of_spec.rb