Module: RspecRequestHelpers::Helpers
- Defined in:
- lib/rspec_request_helpers/helpers.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
- .generate_helpers ⇒ Object (also: regenerate_helpers)
- .included(klass) ⇒ Object
Instance Method Summary collapse
- #assert_hash ⇒ Object
- #assert_object ⇒ Object
- #assert_raw ⇒ Object
- #object(hash) ⇒ Object
- #parse_json(json_data) ⇒ Object
- #response_body ⇒ Object
- #response_hash ⇒ Object
- #response_objects ⇒ Object
Class Method Details
.generate_helpers ⇒ Object Also known as: regenerate_helpers
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/rspec_request_helpers/helpers.rb', line 63 def self.generate_helpers %i(get post put patch delete).each do |http_verb| define_method :"do_#{http_verb}" do is_not_get = http_verb != :get is_json = headers['Content-Type'] == 'application/json' is_new_rails = 3 < Rails::VERSION::MAJOR params_require_normalization = is_new_rails && is_json && is_not_get normalized_params = params_require_normalization ? params.to_json : params if 4 < Rails::VERSION::MAJOR public_send(http_verb, path, params: normalized_params, headers: headers) else public_send(http_verb, path, normalized_params, headers) end end RspecRequestHelpers.configuration.content_types.each do |type, mime_type| RspecRequestHelpers.configuration.symbols_with_status_codes.each do |status, code| %i(raw hash object).each do |resp_assertion| define_method :"assert_#{code}_#{type}" do expect(response).to have_http_status(status) expect(response.content_type).to eq mime_type end define_method :"assert_#{code}_#{type}_#{resp_assertion}" do public_send(:"assert_#{code}_#{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 |
.included(klass) ⇒ Object
27 28 29 |
# File 'lib/rspec_request_helpers/helpers.rb', line 27 def self.included(klass) klass.extend ClassMethods end |
Instance Method Details
#assert_hash ⇒ Object
59 60 61 |
# File 'lib/rspec_request_helpers/helpers.rb', line 59 def assert_hash expect(response_hash).to eq(expected_response) end |
#assert_object ⇒ Object
55 56 57 |
# File 'lib/rspec_request_helpers/helpers.rb', line 55 def assert_object expect(response_objects).to have_attributes(expected_response) end |
#assert_raw ⇒ Object
51 52 53 |
# File 'lib/rspec_request_helpers/helpers.rb', line 51 def assert_raw expect(response_body).to eq expected_response end |
#object(hash) ⇒ Object
31 32 33 |
# File 'lib/rspec_request_helpers/helpers.rb', line 31 def object(hash) OpenStruct.new(hash) end |
#parse_json(json_data) ⇒ Object
35 36 37 |
# File 'lib/rspec_request_helpers/helpers.rb', line 35 def parse_json(json_data) JSON.parse(json_data, symbolize_names: true) end |
#response_body ⇒ Object
39 40 41 |
# File 'lib/rspec_request_helpers/helpers.rb', line 39 def response_body response.body end |
#response_hash ⇒ Object
43 44 45 |
# File 'lib/rspec_request_helpers/helpers.rb', line 43 def response_hash parse_json(response_body) end |
#response_objects ⇒ Object
47 48 49 |
# File 'lib/rspec_request_helpers/helpers.rb', line 47 def response_objects JSON.parse(response_body, object_class: OpenStruct) end |