Sha256: b01c6f1e28314d5f8913432023a468ea6c01c0f7694ecd58dc9afc2ab888a6d8

Contents?: true

Size: 1.79 KB

Versions: 16

Compression:

Stored size: 1.79 KB

Contents

require 'spec_helper'

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

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

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

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

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

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

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

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

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

    context 'when no mutually exclusive params are present' do
      let(:params) { { beer: true, somethingelse: true } }

      it 'params' do
        expect(validator.validate!(params)).to eql params
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
grape-1.0.1 spec/grape/validations/validators/mutual_exclusion_spec.rb
grape-1.0.0 spec/grape/validations/validators/mutual_exclusion_spec.rb
grape-0.19.2 spec/grape/validations/validators/mutual_exclusion_spec.rb
grape-0.19.1 spec/grape/validations/validators/mutual_exclusion_spec.rb
grape-0.19.0 spec/grape/validations/validators/mutual_exclusion_spec.rb
grape-0.18.0 spec/grape/validations/validators/mutual_exclusion_spec.rb
grape-0.17.0 spec/grape/validations/validators/mutual_exclusion_spec.rb
grape-0.16.2 spec/grape/validations/validators/mutual_exclusion_spec.rb
grape-0.16.1 spec/grape/validations/validators/mutual_exclusion_spec.rb
grape-0.15.0 spec/grape/validations/validators/mutual_exclusion_spec.rb
grape-0.14.0 spec/grape/validations/validators/mutual_exclusion_spec.rb
grape-0.13.0 spec/grape/validations/validators/mutual_exclusion_spec.rb
grape-0.12.0 spec/grape/validations/validators/mutual_exclusion_spec.rb
grape-0.11.0 spec/grape/validations/validators/mutual_exclusion_spec.rb
grape-0.10.1 spec/grape/validations/validators/mutual_exclusion_spec.rb
grape-0.10.0 spec/grape/validations/validators/mutual_exclusion_spec.rb