• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ruby-grape / grape / 9050471849

12 May 2024 09:42AM UTC coverage: 99.135% (+0.008%) from 99.127%
9050471849

Pull #2437

github

web-flow
Merge e8210d590 into 3f6a70ae4
Pull Request #2437: Add length validator

127 of 127 new or added lines in 1 file covered. (100.0%)

14100 of 14223 relevant lines covered (99.14%)

181.21 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.87
/spec/grape/exceptions/validation_errors_spec.rb
1
# frozen_string_literal: true
2

3
describe Grape::Exceptions::ValidationErrors do
69✔
4
  let(:validation_message) { 'FooBar is invalid' }
276✔
5
  let(:validation_error) { instance_double Grape::Exceptions::Validation, params: [validation_message], message: '' }
276✔
6

7
  context 'initialize' do
69✔
8
    subject do
69✔
9
      described_class.new(errors: [validation_error], headers: headers)
69✔
10
    end
11

12
    let(:headers) do
69✔
13
      {
69✔
14
        'A-Header-Key' => 'A-Header-Value'
15
      }
16
    end
17

18
    it 'assigns headers through base class' do
69✔
19
      expect(subject.headers).to eq(headers)
69✔
20
    end
21
  end
22

23
  context 'message' do
69✔
24
    context 'is not repeated' do
69✔
25
      subject(:message) { error.message.split(',').map(&:strip) }
207✔
26

27
      let(:error) do
69✔
28
        described_class.new(errors: [validation_error, validation_error])
138✔
29
      end
30

31
      it { expect(message).to include validation_message }
138✔
32
      it { expect(message.size).to eq 1 }
138✔
33
    end
34
  end
35

36
  describe '#full_messages' do
69✔
37
    context 'with errors' do
69✔
38
      subject { described_class.new(errors: [validation_error_1, validation_error_2]).full_messages }
138✔
39

40
      let(:validation_error_1) { Grape::Exceptions::Validation.new(params: ['id'], message: :presence) }
138✔
41
      let(:validation_error_2) { Grape::Exceptions::Validation.new(params: ['name'], message: :presence) }
138✔
42

43
      it 'returns an array with each errors full message' do
69✔
44
        expect(subject).to contain_exactly('id is missing', 'name is missing')
69✔
45
      end
46
    end
47

48
    context 'when attributes is an array of symbols' do
69✔
49
      subject { described_class.new(errors: [validation_error]).full_messages }
138✔
50

51
      let(:validation_error) { Grape::Exceptions::Validation.new(params: [:admin_field], message: 'Can not set admin-only field') }
138✔
52

53
      it 'returns an array with an error full message' do
69✔
54
        expect(subject.first).to eq('admin_field Can not set admin-only field')
69✔
55
      end
56
    end
57
  end
58

59
  context 'api' do
69✔
60
    subject { Class.new(Grape::API) }
138✔
61

62
    def app
69✔
63
      subject
69✔
64
    end
65

66
    it 'can return structured json with separate fields' do
69✔
67
      subject.format :json
69✔
68
      subject.rescue_from described_class do |e|
69✔
69
        error!(e, 400)
69✔
70
      end
71
      subject.params do
69✔
72
        optional :beer
69✔
73
        optional :wine
69✔
74
        optional :juice
69✔
75
        exactly_one_of :beer, :wine, :juice
69✔
76
      end
77
      subject.get '/exactly_one_of' do
69✔
78
        'exactly_one_of works!'
×
79
      end
80
      get '/exactly_one_of', beer: 'string', wine: 'anotherstring'
69✔
81
      expect(last_response).to be_bad_request
69✔
82
      expect(JSON.parse(last_response.body)).to eq(
69✔
83
        [
84
          'params' => %w[beer wine],
85
          'messages' => ['are mutually exclusive']
86
        ]
87
      )
88
    end
89
  end
90
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc