Skip to content

Commit

Permalink
Adding specs
Browse files Browse the repository at this point in the history
  • Loading branch information
msroot committed Oct 28, 2016
1 parent ca51877 commit f4454d7
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
45 changes: 45 additions & 0 deletions spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,51 @@ def app
end
end

describe '#params' do
context 'access params with HashWithIndifferentAccess' do
it 'params' do
subject.params do
requires :a, type: String
end
subject.get '/foo' do
[params[:a], params['a'], params["a"]]
end

get '/foo', a: 'bar'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('["bar", "bar", "bar"]')
end
end

context 'to_h should a Hash' do
it 'to_h' do
subject.get '/foo' do
params.to_h.class
end

get '/foo'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('Hash')
end
end

context 'sets a value to params' do
it 'params' do
subject.params do
requires :a, type: String
end
subject.get '/foo' do
params[:a] = 'bar'
end

get '/foo', a: 'foo'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq("bar")
end
end
end


describe '#declared' do
before do
subject.params do
Expand Down
12 changes: 6 additions & 6 deletions spec/grape/validations/validators/coerce_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -625,35 +625,35 @@ class User

get '/', a: %w(the other)
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('#<Hashie::Array ["the", "other"]>')
expect(last_response.body).to eq('["the", "other"]')

get '/', a: { a: 1, b: 2 }
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('a is invalid')

get '/', a: [1, 2, 3]
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('#<Hashie::Array ["1", "2", "3"]>')
expect(last_response.body).to eq('["1", "2", "3"]')
end

it 'allows multiple collection types' do
get '/', b: [1, 2, 3]
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('#<Hashie::Array [1, 2, 3]>')
expect(last_response.body).to eq('[1, 2, 3]')

get '/', b: %w(1 2 3)
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('#<Hashie::Array [1, 2, 3]>')
expect(last_response.body).to eq('[1, 2, 3]')

get '/', b: [1, true, 'three']
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('#<Hashie::Array ["1", "true", "three"]>')
expect(last_response.body).to eq('["1", "true", "three"]')
end

it 'allows collections with multiple types' do
get '/', c: [1, '2', true, 'three']
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('#<Hashie::Array [1, 2, "true", "three"]>')
expect(last_response.body).to eq('[1, 2, "true", "three"]')

get '/', d: '1'
expect(last_response.status).to eq(200)
Expand Down

0 comments on commit f4454d7

Please sign in to comment.