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



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/rspec_request_helpers/helpers.rb', line 96

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

.included(klass) ⇒ Object



60
61
62
# File 'lib/rspec_request_helpers/helpers.rb', line 60

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

Instance Method Details

#assert_bodyObject



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

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

#assert_response_bodyObject



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

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

#assert_response_objectObject



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

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

#object(hash) ⇒ Object



64
65
66
# File 'lib/rspec_request_helpers/helpers.rb', line 64

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

#parse_json(json_data) ⇒ Object



68
69
70
# File 'lib/rspec_request_helpers/helpers.rb', line 68

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

#response_bodyObject



72
73
74
# File 'lib/rspec_request_helpers/helpers.rb', line 72

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

#response_objectObject



76
77
78
# File 'lib/rspec_request_helpers/helpers.rb', line 76

def response_object
  OpenStruct.new(responce_body)
end

#response_objectsObject



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

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