Response body value validation

Add the possibility to pass a block to the run_test!
method in order to add expectations on your response
This commit is contained in:
Horia Radu
2017-04-28 11:40:33 +03:00
parent cf9170101b
commit 51c9f4e5e6
7 changed files with 44 additions and 11 deletions

View File

@@ -103,6 +103,16 @@ 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).
If you want to do additional validation on the JSON response, pass a block to the __run_test!__ method:
```ruby
response '201', 'blog created' do
run_test! do |body|
expect(body['title']).to eq('foo')
end
end
```
If you'd like your specs to be a little more explicit about what's going on here, you can replace the call to __run_test!__ with equivalent "before" and "it" blocks:
```ruby