You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Or use `OpenapiFirst::Test.app(MyApp)` to observe your app during tests. See [Contract testing](#contract-testing) for details.
27
-
28
26
## Contents
29
27
30
28
<!-- TOC -->
31
29
32
-
-[Contract testing](#contract-testing)
33
30
-[Rack Middlewares](#rack-middlewares)
34
31
-[Request validation](#request-validation)
35
32
-[Response validation](#response-validation)
36
-
-[Test assertions](#test-assertions)
33
+
-[Contract testing](#contract-testing)
34
+
-[Test assertions](#test-assertions)
37
35
-[Manual use](#manual-use)
38
36
-[Framework integration](#framework-integration)
39
37
-[Configuration](#configuration)
@@ -46,78 +44,6 @@ Or use `OpenapiFirst::Test.app(MyApp)` to observe your app during tests. See [Co
46
44
47
45
<!-- /TOC -->
48
46
49
-
## Contract Testing
50
-
51
-
You can see your OpenAPI API description as a contract that your clients can rely on as how your API behaves. There are two aspects of contract testing: Validation and Coverage. By validating requests and responses, you can avoid that your API implementation processes requests or returns responses that don't match your API description. To make sure your _whole_ API description is implemented, openapi_first can check that all of your API description is covered when you test your API with [rack-test](https://github.com/rack/rack-test).
52
-
53
-
Here is how to set it up:
54
-
55
-
1. Register all OpenAPI documents to track coverage for.
56
-
This should go at the top of your test helper file before loading your application code.
57
-
```ruby
58
-
require 'openapi_first'
59
-
OpenapiFirst::Test.setup do |config|
60
-
config.register('openapi/openapi.yaml')
61
-
end
62
-
```
63
-
2. Observe your application. You can do this in multiple ways:
64
-
- Add an `app` method to your tests, which wraps your application with silent request / response validation. (✷1)
65
-
```ruby
66
-
moduleRequestSpecHelpers
67
-
defapp
68
-
OpenapiFirst::Test.app(MyApp)
69
-
end
70
-
end
71
-
72
-
RSpec.configure do |config|
73
-
config.includeRequestSpecHelpers, type::request
74
-
end
75
-
```
76
-
77
-
Ordo this by creating a Moduleand including it to add an "app" method.
4. Run your tests. TheCoverage feature will tell you about missing or invalid requests/responses.
85
-
86
-
(✷1): It does not matter what method of openapi_first you use to validate requests/responses. Instead of using`OpenapiFirstTest.app` to wrap your application, you could also use the [middlewares](#rack-middlewares) or [test assertion method](#test-assertions), but you would have to do that for all requests/responses defined in your API description to make coverage work.
87
-
88
-
### Configure test coverage
89
-
90
-
OpenapiFirst::Test raises an error when a request is not defined. You can deactivate this with:
91
-
92
-
```ruby
93
-
OpenapiFirst::Test.setup do |test|
94
-
# …
95
-
test.ignore_unknown_requests = true
96
-
end
97
-
```
98
-
99
-
Exclude certain _responses_ from coverage with `skip_coverage`:
100
-
101
-
```ruby
102
-
OpenapiFirst::Test.setup do |test|
103
-
# …
104
-
test.skip_response_coverage do |response_definition|
105
-
response_definition.status == '5XX'
106
-
end
107
-
end
108
-
```
109
-
110
-
Skip coverage for a request and all responses alltogether of a route with `skip_coverage`:
@@ -227,7 +153,79 @@ use OpenapiFirst::Middlewares::ResponseValidation, 'openapi.yaml', raise_error:
227
153
228
154
If you are adopting OpenAPI you can use these options together with [hooks](#hooks) to get notified about requests/responses that do match your API description.
229
155
230
-
## Test assertions
156
+
## Contract Testing
157
+
158
+
You can see your OpenAPI API description as a contract that your clients can rely on as how your API behaves. There are two aspects of contract testing: Validation and Coverage. By validating requests and responses, you can avoid that your API implementation processes requests or returns responses that don't match your API description. To make sure your _whole_ API description is implemented, openapi_first can check that all of your API description is covered when you test your API with [rack-test](https://github.com/rack/rack-test).
159
+
160
+
Here is how to set it up:
161
+
162
+
1. Register all OpenAPI documents to track coverage for.
163
+
This should go at the top of your test helper file before loading your application code.
164
+
```ruby
165
+
require 'openapi_first'
166
+
OpenapiFirst::Test.setup do |config|
167
+
config.register('openapi/openapi.yaml')
168
+
end
169
+
```
170
+
2. Observe your application. You can do this in multiple ways:
171
+
- Add an `app` method to your tests, which wraps your application with silent request / response validation. (✷1)
172
+
```ruby
173
+
moduleRequestSpecHelpers
174
+
defapp
175
+
OpenapiFirst::Test.app(MyApp)
176
+
end
177
+
end
178
+
179
+
RSpec.configure do |config|
180
+
config.includeRequestSpecHelpers, type::request
181
+
end
182
+
```
183
+
184
+
Ordo this by creating a Moduleand including it to add an "app" method.
4. Run your tests. TheCoverage feature will tell you about missing or invalid requests/responses.
192
+
193
+
(✷1): It does not matter what method of openapi_first you use to validate requests/responses. Instead of using`OpenapiFirstTest.app` to wrap your application, you could also use the [middlewares](#rack-middlewares) or [test assertion method](#test-assertions), but you would have to do that for all requests/responses defined in your API description to make coverage work.
194
+
195
+
### Configure test coverage
196
+
197
+
OpenapiFirst::Test raises an error when a request is not defined. You can deactivate this with:
198
+
199
+
```ruby
200
+
OpenapiFirst::Test.setup do |test|
201
+
# …
202
+
test.ignore_unknown_requests = true
203
+
end
204
+
```
205
+
206
+
Exclude certain _responses_ from coverage with `skip_coverage`:
207
+
208
+
```ruby
209
+
OpenapiFirst::Test.setup do |test|
210
+
# …
211
+
test.skip_response_coverage do |response_definition|
212
+
response_definition.status == '5XX'
213
+
end
214
+
end
215
+
```
216
+
217
+
Skip coverage for a request and all responses alltogether of a route with `skip_coverage`:
openapi_first ships with a simple but powerful Test method to run request and response validation in your tests without using the middlewares. This is designed to be used with rack-testorRuby on Rails integration tests or request specs.
0 commit comments