Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ketiko/open_api-rspec
https://github.com/ketiko/open_api-rspec
openapi openapi-validation rspec-matchers
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/ketiko/open_api-rspec
- Owner: ketiko
- License: mit
- Created: 2018-02-25T17:46:31.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-04-12T05:35:41.000Z (over 1 year ago)
- Last Synced: 2024-07-21T15:51:15.179Z (4 months ago)
- Topics: openapi, openapi-validation, rspec-matchers
- Language: Ruby
- Size: 94.7 KB
- Stars: 7
- Watchers: 3
- Forks: 2
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# OpenApi::RSpec [![Build Status](https://travis-ci.org/ketiko/open_api-rspec.svg?branch=master)](https://travis-ci.org/ketiko/open_api-rspec)
RSpec matchers and shared examples for OpenApi
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'open_api-rspec'
```And then execute:
$ bundle
Or install it yourself as:
$ gem install open_api-rspec
## Usage
Add a file with the following:
```ruby
#file: spec/support/openapi_schema.rbRSpec.shared_context "Shared OpenAPI JSON" do
let(:open_api_json) { '{my open api json schema'} }
endRSpec.configure do |rspec|
rspec.include_context "Shared OpenAPI JSON", type: :request
rspec.include_context "Shared OpenAPI JSON", type: :controller
end
```Test if your swagger.json endpoint is a valid schema.
```ruby
require 'rails_helper'RSpec.describe SwaggerController, type: :request do
describe '#swagger' do
before do
get '/swagger.json'
endit { expect(response).to have_http_status(:ok) }
it { expect(response.body).to be_valid_openapi_schema }
end
end
```Test that a response body matches a specific OpenApi Schema.
```ruby
require 'rails_helper'RSpec.describe UsersController, type: :request do
describe '#show' do
before do
get '/users/1'
endit { expect(response).to have_http_status(:ok) }
it { expect(response).to match_openapi_response_schema :User }
end
end
```
Test that a request spec matches an OpenApi Schema.
This will check body, path, query, form data params.
It will also fail if any unknown params are passed.
It checks that there is a documented response code and verifies its response schema
```ruby
require 'rails_helper'RSpec.describe UsersController, type: :request do
describe '#show' do
before do
get '/users/1'
endit { expect(response).to have_http_status(:ok) }
it { expect(response).to match_openapi_response_schema :User }
it_behaves_like :an_openapi_endpoint
end
end
```## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/open_api-rspec.