Skip to content

Commit a43fea9

Browse files
author
Braktar
committed
Add array spec
1 parent 990c3a2 commit a43fea9

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

spec/grape/validations/validators/coerce_spec.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,44 @@ def self.parse(_val)
706706
expect(JSON.parse(last_response.body)).to eq([1, 1, 1, 1])
707707
end
708708

709+
context 'Array type and coerce_with should' do
710+
before do
711+
subject.params do
712+
optional :arr, type: Array, coerce_with: (lambda do |val|
713+
if val.nil?
714+
[]
715+
else
716+
val
717+
end
718+
end)
719+
end
720+
subject.get '/' do
721+
params[:arr].class.to_s
722+
end
723+
end
724+
725+
it 'coerce nil value to array' do
726+
get '/', arr: nil
727+
728+
expect(last_response.status).to eq(200)
729+
expect(last_response.body).to eq('Array')
730+
end
731+
732+
it 'not coerce missing field' do
733+
get '/'
734+
735+
expect(last_response.status).to eq(200)
736+
expect(last_response.body).to eq('NilClass')
737+
end
738+
739+
it 'coerce array as array' do
740+
get '/', arr: []
741+
742+
expect(last_response.status).to eq(200)
743+
expect(last_response.body).to eq('Array')
744+
end
745+
end
746+
709747
it 'uses parse where available' do
710748
subject.params do
711749
requires :ints, type: Array, coerce_with: JSON do

0 commit comments

Comments
 (0)