Skip to content

Instantly share code, notes, and snippets.

@izszzz
Last active July 7, 2022 13:23
Show Gist options
  • Save izszzz/4165f2a648fcc696594bbb0e157d4fd3 to your computer and use it in GitHub Desktop.
Save izszzz/4165f2a648fcc696594bbb0e157d4fd3 to your computer and use it in GitHub Desktop.
respond_with.rb
# frozen_string_literal: true
RSpec.shared_examples 'respond_with' do |http_status, openapi: false|
# openapi
openapi = { summary: metadata[:example_group][:parent_example_group][:parent_example_group][:description] } if openapi
describe '', openapi: openapi do
let(:id) do
super()
rescue StandardError
nil
end
let(:params) do
super()
rescue StandardError
nil
end
it do
process method, url(id:), params:, as: format
expect(response).to have_http_status(http_status)
end
end
def description
RSpec.current_example.metadata[:example_group][:parent_example_group][:parent_example_group][:parent_example_group][:description]
end
def format
value = description.split(' ')[2] || nil
value ? value.to_sym : nil
end
def action
description.split(' ')[1].gsub(%r{/}, '')
end
def method
description.split(' ')[0].downcase
end
def controller
self.class.top_level_description.name.downcase
end
def url(id: nil, params: nil)
url_for controller:, action:, id:
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment