Sha256: 4405c288ff28213c9901a41129e21a82121acaa940bde943c114e9d578a41e3e

Contents?: true

Size: 1.73 KB

Versions: 8

Compression:

Stored size: 1.73 KB

Contents

require 'spec_helper'

describe Grape::Validations::AllOrNoneOfValidator do
  describe '#validate!' do
    let(:scope) do
      Struct.new(:opts) do
        def params(arg)
          arg
        end

        def required?; end
      end
    end
    let(:all_or_none_params) { %i[beer wine grapefruit] }
    let(:validator) { described_class.new(all_or_none_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 none of the restricted params is selected' do
      let(:params) { { somethingelse: true } }

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

    context 'when only a subset of restricted 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
      context 'mixed with other params' do
        let(:mixed_params) { params.merge!(other: true, andanother: true) }

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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
grape-1.2.4 spec/grape/validations/validators/all_or_none_spec.rb
grape-1.2.3 spec/grape/validations/validators/all_or_none_spec.rb
grape-1.2.2 spec/grape/validations/validators/all_or_none_spec.rb
grape-1.2.1 spec/grape/validations/validators/all_or_none_spec.rb
grape-1.2.0 spec/grape/validations/validators/all_or_none_spec.rb
grape-1.1.0 spec/grape/validations/validators/all_or_none_spec.rb
grape-1.0.3 spec/grape/validations/validators/all_or_none_spec.rb
grape-1.0.2 spec/grape/validations/validators/all_or_none_spec.rb