Module: RSpec::Rails::Swagger::Helpers::Operation

Defined in:
lib/rspec/rails/swagger/helpers.rb

Instance Method Summary collapse

Instance Method Details

#consumes(*mime_types) ⇒ Object



169
170
171
# File 'lib/rspec/rails/swagger/helpers.rb', line 169

def consumes *mime_types
  [:swagger_operation][:consumes] = mime_types
end

#produces(*mime_types) ⇒ Object



173
174
175
# File 'lib/rspec/rails/swagger/helpers.rb', line 173

def produces *mime_types
  [:swagger_operation][:produces] = mime_types
end

#response(status_code, attributes = {}, &block) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/rspec/rails/swagger/helpers.rb', line 177

def response status_code, attributes = {}, &block
  attributes.symbolize_keys!

  validate_status_code! status_code
  validate_description! attributes[:description]

  meta = {
    swagger_object: :response,
    swagger_response: attributes.merge(status_code: status_code)
  }
  describe(status_code, meta) do
    self.module_exec(&block) if block_given?

    # To make a request we need:
    # - the details we've collected in the metadata
    # - parameter values defined using let()
    # RSpec tries to limit access to metadata inside of it() / before()
    # / after() blocks but that scope is the only place you can access
    # the let() values. The solution the swagger_rails dev came up with
    # is to use the example.metadata passed into the block with the
    # block's scope which has access to the let() values.
    before do |example|
      builder = RequestBuilder.new(example., self)
      method = builder.method
      path = [builder.path, builder.query].join
      headers = builder.headers
      body = builder.body

      # Run the request
      if ::Rails::VERSION::MAJOR >= 5
        self.send(method, path, {params: body, headers: headers})
      else
        self.send(method, path, body, headers)
      end

      if example.[:capture_examples]
        examples = example.[:swagger_response][:examples] ||= {}
        examples[response.content_type.to_s] = response.body
      end
    end

    # TODO: see if we can get the caller to show up in the error
    # backtrace for this test.
    it("returns the correct status code") do
      expect(response).to have_http_status(status_code)
    end
  end
end