README.md in rspec-request_describer-0.5.0 vs README.md in rspec-request_describer-0.6.0

- old
+ new

@@ -10,66 +10,77 @@ - [rack-test](https://github.com/rack-test/rack-test) - [rspec-rails](https://github.com/rspec/rspec-rails) ## Setup -### Install - Add this line to your application's Gemfile: ```ruby -gem 'rspec-request_describer' +group :test do + gem 'rspec-request_describer' +end ``` -And then execute: +And then include `RSpec::RequestDescriber`: -``` -bundle -``` - -Or install it yourself as: - -``` -gem install rspec-request_describer -``` - -### Include - -Include `RSpec::RequestDescriber` to your example groups like this: - ```ruby -require 'rspec/request_describer' - +# spec/rails_helper.rb RSpec.configure do |config| config.include RSpec::RequestDescriber, type: :request end ``` ## Usage -Note that this is an example in a Rails app. +Write HTTP method and URL path in the top-level description of your request-specs. -### subject +```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 +``` -`RSpec::RequestDescriber` provides `subject` from its top-level description. +Internally, `RSpec::RequestDescriber` defines `subject` and some `let` from its top-level description like this: ```ruby -# subject will be `get('/users')`. RSpec.describe 'GET /users' do + subject do + __send__(http_method, path, headers:, params:) + end + + let(:http_method) do + 'get' + end + + let(:path) do + '/users' + end + + let(:headers) do + {} + end + + let(:params) do + {} + end + it 'returns 200' do subject expect(response).to have_http_status(200) end end ``` ### headers -If you want to modify request headers, change `headers` before calling `subject`. +If you want to modify request headers, change `headers`: ```ruby -# `subject` will be `get('/users', headers: { 'Authorization' => 'token 12345' })`. RSpec.describe 'GET /users' do context 'with Authorization header' do before do headers['Authorization'] = 'token 12345' end @@ -82,14 +93,13 @@ end ``` ### params -If you want to modify request parameters, change `params` before calling `subject`. +If you want to modify request parameters, change `params`: ```ruby -# `subject` will be `get('/users', params: { 'sort' => 'id' })`. RSpec.describe 'GET /users' do context 'with sort parameter' do before do params['sort'] = 'id' end @@ -109,13 +119,12 @@ ``` ### path parameters You can embed variables in URL path like `/users/:user_id`. -In this example, the returned value of `user_id` method will be embedded as its real value. +In this example, the returned value from `#user_id` method will be embedded as its real value. ```ruby -# `subject` will be `get("/users/#{user_id}")`. RSpec.describe 'GET /users/:user_id' do let(:user) do User.create(name: 'alice') end