File: instance_behaivour_spec.rb

package info (click to toggle)
ruby-grape 1.6.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,156 kB
  • sloc: ruby: 25,265; makefile: 7
file content (45 lines) | stat: -rw-r--r-- 1,251 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# frozen_string_literal: true

require 'spec_helper'

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