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



57
58
59
60
61
62
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
# File 'lib/rspec_request_helpers/helpers.rb', line 57

def self.generate_helpers
  %i(get post put patch delete).each do |http_verb|
    define_method :"do_#{http_verb}" do
      is_not_json_get = (http_verb == :get || headers['Content-Type'] != 'application/json')
      params_require_normalization = Rails::VERSION::MAJOR < 4 && is_not_json_get
      normalized_params = params_require_normalization ? params : params.to_json
      if Rails::VERSION::MAJOR >= 5
        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



21
22
23
# File 'lib/rspec_request_helpers/helpers.rb', line 21

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

Instance Method Details

#assert_hashObject



53
54
55
# File 'lib/rspec_request_helpers/helpers.rb', line 53

def assert_hash
  expect(response_hash).to eq(expected_response)
end

#assert_objectObject



49
50
51
# File 'lib/rspec_request_helpers/helpers.rb', line 49

def assert_object
  expect(response_objects).to have_attributes(expected_response)
end

#assert_rawObject



45
46
47
# File 'lib/rspec_request_helpers/helpers.rb', line 45

def assert_raw
  expect(response_body).to eq expected_response
end

#object(hash) ⇒ Object



25
26
27
# File 'lib/rspec_request_helpers/helpers.rb', line 25

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

#parse_json(json_data) ⇒ Object



29
30
31
# File 'lib/rspec_request_helpers/helpers.rb', line 29

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

#response_bodyObject



33
34
35
# File 'lib/rspec_request_helpers/helpers.rb', line 33

def response_body
  response.body
end

#response_hashObject



37
38
39
# File 'lib/rspec_request_helpers/helpers.rb', line 37

def response_hash
  parse_json(response_body)
end

#response_objectsObject



41
42
43
# File 'lib/rspec_request_helpers/helpers.rb', line 41

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