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



184
185
186
# File 'lib/rspec/rails/swagger/helpers.rb', line 184

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

#produces(*mime_types) ⇒ Object



188
189
190
# File 'lib/rspec/rails/swagger/helpers.rb', line 188

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

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



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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/rspec/rails/swagger/helpers.rb', line 197

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
      env = builder.env
      body = builder.body

      # Run the request
      if ::Rails::VERSION::MAJOR >= 5
        self.send(method, path, {params: body, headers: headers, env: env})
      else
        self.send(method, path, body, headers.merge(env))
      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

#tags(*tags) ⇒ Object



192
193
194
195
# File 'lib/rspec/rails/swagger/helpers.rb', line 192

def tags *tags
  [:swagger_operation][:tags] ||= []
  [:swagger_operation][:tags] += tags
end