yield entire response instead of only the body

This commit is contained in:
Horia Radu 2017-04-29 12:06:57 +03:00
parent 51c9f4e5e6
commit 37f86f6d94
3 changed files with 7 additions and 5 deletions

View File

@ -103,12 +103,13 @@ If you've used [Swagger](http://swagger.io/specification) before, then the synta
Take special note of the __run_test!__ method that's called within each response block. This tells rswag to create and execute a corresponding example. It builds and submits a request based on parameter descriptions and corresponding values that have been provided using the rspec "let" syntax. For example, the "post" description in the example above specifies a "body" parameter called "blog". It also lists 2 different responses. For the success case (i.e. the 201 response), notice how "let" is used to set the blog parameter to a value that matches the provided schema. For the failure case (i.e. the 422 response), notice how it's set to a value that does not match the provided schema. When the test is executed, rswag also validates the actual response code and, where applicable, the response body against the provided [JSON Schema](http://json-schema.org/documentation.html). Take special note of the __run_test!__ method that's called within each response block. This tells rswag to create and execute a corresponding example. It builds and submits a request based on parameter descriptions and corresponding values that have been provided using the rspec "let" syntax. For example, the "post" description in the example above specifies a "body" parameter called "blog". It also lists 2 different responses. For the success case (i.e. the 201 response), notice how "let" is used to set the blog parameter to a value that matches the provided schema. For the failure case (i.e. the 422 response), notice how it's set to a value that does not match the provided schema. When the test is executed, rswag also validates the actual response code and, where applicable, the response body against the provided [JSON Schema](http://json-schema.org/documentation.html).
If you want to do additional validation on the JSON response, pass a block to the __run_test!__ method: If you want to do additional validation on the response, pass a block to the __run_test!__ method:
```ruby ```ruby
response '201', 'blog created' do response '201', 'blog created' do
run_test! do |body| run_test! do |response|
expect(body['title']).to eq('foo') data = JSON.parse(response)
expect(data['title']).to eq('foo')
end end
end end
``` ```

View File

@ -48,7 +48,7 @@ module Rswag
raise UnexpectedResponse, "Expected response body to match schema: #{ex.message}" raise UnexpectedResponse, "Expected response body to match schema: #{ex.message}"
end end
block.call(JSON.parse(body)) if block_given? block.call(body) if block_given?
end end
end end

View File

@ -46,7 +46,8 @@ module Rswag
context "'block' provided" do context "'block' provided" do
let(:call) do let(:call) do
subject.validate!(response) do |body| subject.validate!(response) do |body|
expect(body['text']).to eq('Some comment') data = JSON.parse(body)
expect(data['text']).to eq('Some comment')
end end
end end