### Setup RSpec::RequestDescriber in Gemfile Source: https://github.com/r7kamura/rspec-request_describer/blob/master/README.md This snippet shows how to add the rspec-request_describer gem to your application's Gemfile for use in the test environment. ```Ruby group :test do gem 'rspec-request_describer' end ``` -------------------------------- ### Basic Request Spec with RSpec::RequestDescriber Source: https://github.com/r7kamura/rspec-request_describer/blob/master/README.md An example of a basic request spec using RSpec::RequestDescriber, where the HTTP method and path are defined in the top-level description. ```Ruby RSpec.describe 'GET /users' do it 'returns 200' do subject expect(response).to have_http_status(200) end end ``` -------------------------------- ### Using Path Parameters in RSpec::RequestDescriber Source: https://github.com/r7kamura/rspec-request_describer/blob/master/README.md This example illustrates how to use path parameters (e.g., `/users/:user_id`) in the top-level description and how to define a `let` variable for the parameter's value. ```Ruby RSpec.describe 'GET /users/:user_id' do let(:user) User.create(name: 'alice') end let(:user_id) user.id end it 'returns 200' do subject expect(response).to have_http_status(200) end end ``` -------------------------------- ### Modifying Request Headers with RSpec::RequestDescriber Source: https://github.com/r7kamura/rspec-request_describer/blob/master/README.md This example shows how to modify the request headers within a spec by changing the `headers` variable, demonstrated with an 'Authorization' header. ```Ruby RSpec.describe 'GET /users' do context 'with Authorization header' do before do headers['Authorization'] = 'token 12345' end it 'returns 200' do subject expect(response).to have_http_status(200) end end end ``` -------------------------------- ### RSpec::RequestDescriber Internal Subject Definition Source: https://github.com/r7kamura/rspec-request_describer/blob/master/README.md This snippet illustrates how RSpec::RequestDescriber internally defines the `subject`, `http_method`, `path`, `headers`, and `params` based on the top-level description. ```Ruby RSpec.describe 'GET /users' do subject do __send__(http_method, path, headers:, params:) end let(:http_method) 'get' end let(:path) '/users' end let(:headers) {} end let(:params) {} end it 'returns 200' do subject expect(response).to have_http_status(200) end end ``` -------------------------------- ### Include RSpec::RequestDescriber in RSpec Configuration Source: https://github.com/r7kamura/rspec-request_describer/blob/master/README.md This code demonstrates how to include the RSpec::RequestDescriber module in your RSpec configuration, specifically for request-type specs. ```Ruby RSpec.configure do |config| config.include RSpec::RequestDescriber, type: :request end ``` -------------------------------- ### Modifying Request Parameters with RSpec::RequestDescriber Source: https://github.com/r7kamura/rspec-request_describer/blob/master/README.md This snippet demonstrates how to modify request parameters within a spec by changing the `params` variable, shown with a 'sort' parameter. ```Ruby RSpec.describe 'GET /users' do context 'with sort parameter' do before do params['sort'] = 'id' end it '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 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.