Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/r7kamura/rspec-request_describer
RSpec plugin to write self-documenting request-specs.
https://github.com/r7kamura/rspec-request_describer
rspec
Last synced: about 21 hours ago
JSON representation
RSpec plugin to write self-documenting request-specs.
- Host: GitHub
- URL: https://github.com/r7kamura/rspec-request_describer
- Owner: r7kamura
- License: mit
- Created: 2014-08-29T01:49:43.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2024-10-28T19:56:51.000Z (3 months ago)
- Last Synced: 2025-01-31T17:18:12.339Z (8 days ago)
- Topics: rspec
- Language: Ruby
- Homepage:
- Size: 81.1 KB
- Stars: 178
- Watchers: 5
- Forks: 15
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# RSpec::RequestDescriber
[![test](https://github.com/r7kamura/rspec-request_describer/actions/workflows/test.yml/badge.svg)](https://github.com/r7kamura/rspec-request_describer/actions/workflows/test.yml)
[![Gem Version](https://badge.fury.io/rb/rspec-request_describer.svg)](https://rubygems.org/gems/rspec-request_describer)RSpec plugin to write self-documenting request-specs.
This gem is designed for:
- [rack-test](https://github.com/rack-test/rack-test)
- [rspec-rails](https://github.com/rspec/rspec-rails)## Setup
Add this line to your application's Gemfile:
```ruby
group :test do
gem 'rspec-request_describer'
end
```And then include `RSpec::RequestDescriber`:
```ruby
# spec/rails_helper.rb
RSpec.configure do |config|
config.include RSpec::RequestDescriber, type: :request
end
```## Usage
Write HTTP method and URL path in the top-level description of your request-specs.
```ruby
# spec/requests/users/index_spec.rb
RSpec.describe 'GET /users' do
it 'returns 200' do
subject
expect(response).to have_http_status(200)
end
end
```Internally, `RSpec::RequestDescriber` defines `subject` and some `let` from its top-level description like this:
```ruby
RSpec.describe 'GET /users' do
subject do
__send__(http_method, path, headers:, params:)
endlet(:http_method) do
'get'
endlet(:path) do
'/users'
endlet(:headers) do
{}
endlet(:params) do
{}
endit 'returns 200' do
subject
expect(response).to have_http_status(200)
end
end
```### headers
If you want to modify request headers, change `headers`:
```ruby
RSpec.describe 'GET /users' do
context 'with Authorization header' do
before do
headers['Authorization'] = 'token 12345'
endit 'returns 200' do
subject
expect(response).to have_http_status(200)
end
end
end
```### params
If you want to modify request parameters, change `params`:
```ruby
RSpec.describe 'GET /users' do
context 'with sort parameter' do
before do
params['sort'] = 'id'
endit 'returns 200 with expected JSON body' do
subject
expect(response).to have_http_status(200)
expect(response.parsed_body).to match(
[
hash_including('id' => 1),
hash_including('id' => 2),
]
)
end
end
end
```### path parameters
You can embed variables in URL path like `/users/:user_id`.
In this example, the returned value from `#user_id` method will be embedded as its real value.```ruby
RSpec.describe 'GET /users/:user_id' do
let(:user) do
User.create(name: 'alice')
endlet(:user_id) do
user.id
endit 'returns 200' do
subject
expect(response).to have_http_status(200)
end
end
```