Sha256: 59ccb1698f2980e8a9ecb5f614fa8b608466b201a96c6f869950e2dd8e617e06

Contents?: true

Size: 1.2 KB

Versions: 3

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

describe 'Validator with instance variables' do
  let(:validator_type) do
    Class.new(Grape::Validations::Validators::Base) do
      def validate_param!(_attr_name, _params)
        if instance_variable_defined?(:@instance_variable) && @instance_variable
          raise Grape::Exceptions::Validation.new(params: ['params'],
                                                  message: 'This should never happen')
        end
        @instance_variable = true
      end
    end
  end
  let(:app) do
    Class.new(Grape::API) do
      params do
        optional :param_to_validate, instance_validator: true
        optional :another_param_to_validate, instance_validator: true
      end
      get do
        'noop'
      end
    end
  end

  before do
    Grape::Validations.register_validator('instance_validator', validator_type)
  end

  after do
    Grape::Validations.deregister_validator('instance_validator')
  end

  it 'passes validation every time' do
    expect(validator_type).to receive(:new).exactly(4).times.and_call_original

    2.times do
      get '/', param_to_validate: 'value', another_param_to_validate: 'value'
      expect(last_response.status).to eq 200
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
grape-1.8.0 spec/grape/validations/instance_behaivour_spec.rb
grape-1.7.1 spec/grape/validations/instance_behaivour_spec.rb
grape-1.7.0 spec/grape/validations/instance_behaivour_spec.rb