Commit f028ba3f by stantoncbradley

add tests for when required array elements are missing

parent a2093577
...@@ -192,8 +192,8 @@ describe RailsParam::Param do ...@@ -192,8 +192,8 @@ describe RailsParam::Param do
end end
end end
describe 'validating array elements' do describe 'validating arrays' do
it 'typecasts array elements' do it 'typecasts array of primitive elements' do
allow(controller).to receive(:params).and_return({'array' => ['1', '2']}) allow(controller).to receive(:params).and_return({'array' => ['1', '2']})
controller.param! :array, Array do |a, i| controller.param! :array, Array do |a, i|
a.param! i, Integer, required: true a.param! i, Integer, required: true
...@@ -216,6 +216,54 @@ describe RailsParam::Param do ...@@ -216,6 +216,54 @@ describe RailsParam::Param do
expect(controller.params['array'][1]['object']['num']).to be_a Integer expect(controller.params['array'][1]['object']['num']).to be_a Integer
expect(controller.params['array'][1]['object']['float']).to be_instance_of Float expect(controller.params['array'][1]['object']['float']).to be_instance_of Float
end end
it 'validates an array of arrays' do
params = {'array' => [[ '1', '2' ],[ '3', '4' ]] }
allow(controller).to receive(:params).and_return(params)
controller.param! :array, Array do |a, i|
a.param! i, Array do |b, e|
b.param! e, Integer, required: true
end
end
expect(controller.params['array'][0][0]).to be_a Integer
expect(controller.params['array'][0][1]).to be_a Integer
expect(controller.params['array'][1][0]).to be_a Integer
expect(controller.params['array'][1][1]).to be_a Integer
end
it 'raises exception when primitive element missing' do
allow(controller).to receive(:params).and_return({'array' => ['1', nil]})
expect {
controller.param! :array, Array do |a, i|
a.param! i, Integer, required: true
end
}.to raise_exception
end
it 'raises exception when nested hash element missing' do
params = {'array' => [{'object'=>{ 'num' => '1', 'float' => nil }},{'object'=>{ 'num' => '2', 'float' => '2.3' }}] }
allow(controller).to receive(:params).and_return(params)
expect {
controller.param! :array, Array do |a|
a.param! :object, Hash do |h|
h.param! :num, Integer, required: true
h.param! :float, Float, required: true
end
end
}.to raise_exception
end
it 'raises exception when nested array element missing' do
params = {'array' => [[ '1', '2' ],[ '3', nil ]] }
allow(controller).to receive(:params).and_return(params)
expect {
controller.param! :array, Array do |a, i|
a.param! i, Array do |b, e|
b.param! e, Integer, required: true
end
end
}.to raise_exception
end
end end
describe "validation" do describe "validation" do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment