Skip to content

Instantly share code, notes, and snippets.

@andriy-baran

andriy-baran/helpers_v2.rb

Last active Jun 27, 2018
Embed
What would you like to do?
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
expect(response_body).to eq(resp_attrs) # collection
end
%i(get post put delete).each do |http_verb|
define_method :"do_#{http_verb}" do
public_send(http_verb, path, valid_params, valid_headers)
end
{ text: 'text/plain', json: 'application/json'}.each do |type, mime_type|
{ not_found: 404, unprocessable_entity: 422, ok: 200 }.each do |status, code|
%i(body response_object parsed_body).each do |resp_assertion|
define_method :"assert_#{code}_#{type}_#{resp_assertion}" do
expect(response).to have_http_status(status)
expect(response.content_type).to eq mime_type
public_send(:"assert_#{resp_assertion}")
end
define_method :"do_#{http_verb}_and_assert_#{code}_#{type}_#{resp_assertion}" do
public_send(:"do_#{http_verb}")
public_send(:"assert_#{code}_#{type}_#{resp_assertion}")
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment