Module: RSpec::Rails::Api::DSL::ExampleGroup

Defined in:
lib/rspec/rails/api/dsl/example_group.rb

Overview

All these methods will be available in example groups (anything but ‘it’, ‘example’, ‘for_code’)

Instance Method Summary collapse

Instance Method Details

#entity(type, fields) ⇒ void

This method returns an undefined value.

Describes an entity

Parameters:

  • type (Symbol)

    Name of the entity for reference

  • fields (Hash)

    Fields declarations



30
31
32
# File 'lib/rspec/rails/api/dsl/example_group.rb', line 30

def entity(type, fields)
  RSpec::Rails::Api::Metadata.add_entity type, fields
end

#for_code(status_code, description = nil, expect_many: nil, expect_one: false, test_only: false, with_content_type: nil, &block) ⇒ void

This method returns an undefined value.

Adds an HTTP code declaration to metadata, with expected result If no expectation is provided, the response will be expected to be empty

Parameters:

  • status_code (Number)

    Status code to test for

  • description (String) (defaults to: nil)

    Description of the route/status pair

  • expect_many (Symbol) (defaults to: nil)

    Check the response for a list of given entity

  • expect_one (Symbol) (defaults to: false)

    Check the response for a given entity

  • with_content_type (String, nil) (defaults to: nil)

    Check for a custom content type instead of using de default one

  • test_only (Boolean) (defaults to: false)

    When true, test the response without filling the documentation



164
165
166
167
168
169
170
171
172
173
174
# File 'lib/rspec/rails/api/dsl/example_group.rb', line 164

def for_code(status_code, description = nil, expect_many: nil, expect_one: false, test_only: false, with_content_type: nil, &block) # rubocop:disable Layout/LineLength, Metrics/ParameterLists
  description ||= Rack::Utils::HTTP_STATUS_CODES[status_code]

  [:rra].add_status_code(status_code, description) unless test_only
  [:rra].add_expectations(expect_one, expect_many, content_type: with_content_type)
  [:rra_current_example] = [:rra].current_example

  describe "->#{test_only ? ' test' : ''} #{status_code} - #{description}" do
    execute_for_code_block(block)
  end
end

#on_delete(url, summary = nil, description = nil) ⇒ void

This method returns an undefined value.

Defines a DELETE action

Parameters:

  • url (String)

    URL to test

  • summary (String) (defaults to: nil)

    What the action does

  • description (String) (defaults to: nil)

    Longer description



147
148
149
# File 'lib/rspec/rails/api/dsl/example_group.rb', line 147

def on_delete(url, summary = nil, description = nil, &)
  on_action(:delete, url, summary, description, &)
end

#on_get(url, summary = nil, description = nil) ⇒ void

This method returns an undefined value.

Defines a GET action

Parameters:

  • url (String)

    URL to test

  • summary (String) (defaults to: nil)

    What the action does

  • description (String) (defaults to: nil)

    Longer description



99
100
101
# File 'lib/rspec/rails/api/dsl/example_group.rb', line 99

def on_get(url, summary = nil, description = nil, &)
  on_action(:get, url, summary, description, &)
end

#on_patch(url, summary = nil, description = nil) ⇒ void

This method returns an undefined value.

Defines a PATCH action

Parameters:

  • url (String)

    URL to test

  • summary (String) (defaults to: nil)

    What the action does

  • description (String) (defaults to: nil)

    Longer description



135
136
137
# File 'lib/rspec/rails/api/dsl/example_group.rb', line 135

def on_patch(url, summary = nil, description = nil, &)
  on_action(:patch, url, summary, description, &)
end

#on_post(url, summary = nil, description = nil) ⇒ void

This method returns an undefined value.

Defines a POST action

Parameters:

  • url (String)

    URL to test

  • summary (String) (defaults to: nil)

    What the action does

  • description (String) (defaults to: nil)

    Longer description



111
112
113
# File 'lib/rspec/rails/api/dsl/example_group.rb', line 111

def on_post(url, summary = nil, description = nil, &)
  on_action(:post, url, summary, description, &)
end

#on_put(url, summary = nil, description = nil) ⇒ void

This method returns an undefined value.

Defines a PUT action

Parameters:

  • url (String)

    URL to test

  • summary (String) (defaults to: nil)

    What the action does

  • description (String) (defaults to: nil)

    Longer description



123
124
125
# File 'lib/rspec/rails/api/dsl/example_group.rb', line 123

def on_put(url, summary = nil, description = nil, &)
  on_action(:put, url, summary, description, &)
end

#parameters(type, fields) ⇒ void

This method returns an undefined value.

Describes request or path parameters

Parameters:

  • type (Symbol)

    Name of the parameters set for reference

  • fields (Hash)

    Fields declarations



41
42
43
# File 'lib/rspec/rails/api/dsl/example_group.rb', line 41

def parameters(type, fields)
  [:rra].add_parameter type, fields
end

#path_params(fields: nil, defined: nil) ⇒ void

This method returns an undefined value.

Declares parameters used in URLS (path) Use fields or defined but not both.

Parameters:

  • fields (Hash, nil) (defaults to: nil)

    An attributes declaration

  • defined (Symbol, nil) (defaults to: nil)

    An entity reference



53
54
55
56
57
58
59
60
61
# File 'lib/rspec/rails/api/dsl/example_group.rb', line 53

def path_params(fields: nil, defined: nil)
  if defined && ![:rra].parameters[defined]
    raise "Parameter #{defined} was not defined with the 'parameters' method"
  end

  fields ||= [:rra].parameters[defined]

  [:rra].add_path_params fields
end

#request_params(attributes: nil, defined: nil) ⇒ void

This method returns an undefined value.

Declares parameters for a request body Use attributes or defined but not both.

Parameters:

  • attributes (Hash, nil) (defaults to: nil)

    An attributes declaration

  • defined (Symbol, nil) (defaults to: nil)

    An entity reference.



71
72
73
74
75
76
77
78
79
# File 'lib/rspec/rails/api/dsl/example_group.rb', line 71

def request_params(attributes: nil, defined: nil)
  if defined && ![:rra].parameters[defined]
    raise "Parameter #{defined} was not defined with the 'parameters' method"
  end

  attributes ||= [:rra].parameters[defined]

  [:rra].add_request_params attributes
end

#requires_security(*scheme_references) ⇒ Object

Declares security schemes valid for this path. It won’t be enforced during testing but will complete the documentation. When the reference does not exist, an exception will be thrown during render, not before.

Parameters:

  • scheme_references (Array<Symbol>)

    References to a security scheme defined with the renderer’s add_security_scheme.



87
88
89
# File 'lib/rspec/rails/api/dsl/example_group.rb', line 87

def requires_security(*scheme_references)
  [:rra].add_security_references(*scheme_references)
end

#resource(name, description = '') ⇒ Object

First method to be called in a spec file # as it will initialize the metadata.

Parameters:

  • name (String)

    Resource name

  • description (String) (defaults to: '')

    Resource description



18
19
20
21
# File 'lib/rspec/rails/api/dsl/example_group.rb', line 18

def resource(name, description = '')
  [:rra] ||= Metadata.new
  [:rra].add_resource name, description
end