Module: RspecRequestHelpers::Helpers

Defined in:
lib/rspec_request_helpers/helpers.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.generate_helpersObject Also known as: regenerate_helpers



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/rspec_request_helpers/helpers.rb', line 115

def self.generate_helpers
  %i(get post put patch delete).each do |http_verb|
    define_method :"do_#{http_verb}" do
      if Rails::VERSION::MAJOR >= 5
        public_send(http_verb, path, params: params, headers: headers)
      else
        public_send(http_verb, path, params, headers)
      end
    end

    RspecRequestHelpers.configuration.content_types.each do |type, mime_type|
      RspecRequestHelpers.configuration.symbols_with_status_codes.each do |status, code|
        %i(body response_object response_body).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



79
80
81
# File 'lib/rspec_request_helpers/helpers.rb', line 79

def self.included(klass)
  klass.extend ClassMethods
end

Instance Method Details

#assert_bodyObject



103
104
105
# File 'lib/rspec_request_helpers/helpers.rb', line 103

def assert_body
  expect(response.body).to eq expected_response
end

#assert_response_bodyObject



111
112
113
# File 'lib/rspec_request_helpers/helpers.rb', line 111

def assert_response_body
  expect(response_body).to eq(expected_response)
end

#assert_response_objectObject



107
108
109
# File 'lib/rspec_request_helpers/helpers.rb', line 107

def assert_response_object
  expect(response_object).to have_attributes(expected_response)
end

#object(hash) ⇒ Object



83
84
85
# File 'lib/rspec_request_helpers/helpers.rb', line 83

def object(hash)
  OpenStruct.new(hash)
end

#parse_json(json_data) ⇒ Object



87
88
89
# File 'lib/rspec_request_helpers/helpers.rb', line 87

def parse_json(json_data)
  JSON.parse(json_data, symbolize_names: true)
end

#response_bodyObject



91
92
93
# File 'lib/rspec_request_helpers/helpers.rb', line 91

def response_body
  JSON.parse(response.body, symbolize_names: true)
end

#response_objectObject



95
96
97
# File 'lib/rspec_request_helpers/helpers.rb', line 95

def response_object
  OpenStruct.new(responce_body)
end

#response_objectsObject



99
100
101
# File 'lib/rspec_request_helpers/helpers.rb', line 99

def response_objects
  JSON.parse(response.body, object_class: OpenStruct)
end