Module: RspecRequestHelpers::Helpers

Defined in:
lib/rspec_request_helpers/helpers.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.generate_helpersObject Also known as: regenerate_helpers



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rspec_request_helpers/helpers.rb', line 35

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: valid_params, headers: valid_headers)
      else
        public_send(http_verb, path, valid_params, valid_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

Instance Method Details

#assert_bodyObject



23
24
25
# File 'lib/rspec_request_helpers/helpers.rb', line 23

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

#assert_response_bodyObject



31
32
33
# File 'lib/rspec_request_helpers/helpers.rb', line 31

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

#assert_response_objectObject



27
28
29
# File 'lib/rspec_request_helpers/helpers.rb', line 27

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

#object(hash) ⇒ Object



3
4
5
# File 'lib/rspec_request_helpers/helpers.rb', line 3

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

#parse_json(json_data) ⇒ Object



7
8
9
# File 'lib/rspec_request_helpers/helpers.rb', line 7

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

#response_bodyObject



11
12
13
# File 'lib/rspec_request_helpers/helpers.rb', line 11

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

#response_objectObject



15
16
17
# File 'lib/rspec_request_helpers/helpers.rb', line 15

def response_object
  OpenStruct.new(responce_body)
end

#response_objectsObject



19
20
21
# File 'lib/rspec_request_helpers/helpers.rb', line 19

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