mirror of
https://github.com/ditkrg/rswag.git
synced 2026-01-23 06:16:42 +00:00
yield entire response instead of only the body
This commit is contained in:
parent
51c9f4e5e6
commit
37f86f6d94
@ -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
|
||||||
```
|
```
|
||||||
|
|||||||
@ -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
|
||||||
|
|
||||||
|
|||||||
@ -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
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user