Skip to content

Instantly share code, notes, and snippets.

View andriy-baran's full-sized avatar
🎯
Focusing

andriy-baran

🎯
Focusing
View GitHub Profile
module Configurable
def config(*attrs)
@configuration ||= Struct.new(*attrs).new
end
def configure
yield(@configuration) if block_given?
end
end
body\code 200 201 403 404 422
raw text/plain | string text/plain | string text/plain | string text/plain | string text/plain | string
object x-json | ostruct x-json | ostruct x-json | ostruct x-json | ostruct x-json | ostruct
collection x-json | hash x-json | hash x-json | hash x-json | hash x-json | hash
RSpec.describe '', type: :request do
include_context 'api session'
<% @path_params.each do |param| %>
<%= "let(#{param}) { #{rand(99..500)} }" %>
<% end %>
let(:path) { "<%= @path %>" }
let(:valid_params) { {} }
let(:valid_response) { {} }
module Rspec
class EndpointGenerator < Rails::Generators::NamedBase
desc 'Generate a spec/requests file for specified action of controller'
argument :controller, type: :string, required: true, banner: 'CONTOLLER'
source_root File.expand_path('../templates', __FILE__)
# https://www.rubytapas.com/2012/11/28/episode-029-redirecting-output/
def capture_output
include_context 'api session'
let(:path) { "/api/v1/#{order_id}/line" }
let(:order_id) { 456 }
context 'when do some complex things' do
let(:order) { FactoryBot.create(:order) }
let(:order_id) { order.id }
let(:item) { LineItem.last }
let(:valid_params) do
{
factory :search_results_resps, class: 'OpenStruct' do
variant_id { variant.id }
product_name { variant.product.name }
product_brand_name { variant.product.brand_name.to_s }
flavor { variant.flavor.to_s }
warehouse_category { variant.product.warehouse_category.to_s }
skip_container { variant.skip_container }
transient do
variant nil
module Helpers
def assert_body
expect(response.body).to eq message # raw body
end
def assert_response_object
expect(response_object).to have_attributes(resp_attrs) # object
end
def assert_response_body
it 'renders correct response' do
get path, valid_params, valid_headers # do request
expect(response).to have_http_status(:ok) #
expect(response.content_type).to eq 'application/json' # Assert status, mime type and response body
expect(response_object).to have_attributes(valid_response) #
item.reload
expect(item.qc_by).to eq nil #
expect(item.qc_signed_by).to eq nil # Check database state
module Helpers
def parse_json(json_data)
JSON.parse(json_data, symbolize_names: true)
end
def response_body
JSON.parse(response.body, symbolize_names: true)
end
def response_object
include_context 'api session'
let(:path) { "/api/v1/#{order_id}/contact_info" }
let(:order_id) { 456 }
it_behaves_like 'protected endpoint', :get
context 'when order found' do
let(:order) { FactoryBot.create(:order, creator: user) }
let(:order_id) { order.id }