#1662 - Add a spec for sending single element to an array #1675
Conversation
Here's an example of a CHANGELOG.md entry: * [#1675](https://github.com/ruby-grape/grape/pull/1675): #1662 - add a spec for sending single element to an array - [@ramkumar-kr](https://github.com/ramkumar-kr).
Generated by |
|
I tried to fix this at no avail. Also tried |
|
You can work-around/hack it with a context 'array params with XML content type' do
let(:single_element_request) do
'<?xml version="1.0" encoding="UTF-8" ?>
<admin>
<products>
<price>100</price>
</products>
</admin>'
end
let(:multiple_element_request) do
'<?xml version="1.0" encoding="UTF-8" ?>
<admin>
<products>
<price>100</price>
</products>
<products>
<price>200</price>
</products>
</admin>'
end
before do
subject.format :xml
subject.content_type :xml, 'application/xml; charset=utf-8'
subject.before do
params[:admin][:products] = [params[:admin][:products]] unless params[:admin][:products].is_a?(Array)
end
subject.params do
requires :admin, type: Hash do
requires :products, type: Array do
requires :price, type: Integer
end
end
end
subject.post do
params[:admin][:products].map do |product|
{ price: product[:price] }
end
end
end
context 'with one element' do
it 'returns a successful response' do
post '/', single_element_request, 'CONTENT_TYPE' => 'application/xml'
expect(last_response.status).to eq(201)
expect(MultiXml.parse(last_response.body)).to eq('objects' => [{ 'price' => 100 }])
end
end
context 'with multiple elements' do
it 'returns a successful response' do
post '/', multiple_element_request, 'CONTENT_TYPE' => 'application/xml'
expect(last_response.status).to eq(201)
expect(MultiXml.parse(last_response.body)).to eq('objects' => [{ 'price' => 100 }, { 'price' => 200 }])
end
end
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
No description provided.