Module: RSpec::Swag::ExampleGroupHelpers

Defined in:
lib/rspec/swag/example_group_helpers.rb

Instance Method Summary collapse

Instance Method Details

#description(value = nil) ⇒ Object

NOTE: ‘description’ requires special treatment because ExampleGroup already defines a method with that name. Provide an override that supports the existing functionality while also setting the appropriate metadata if applicable



29
30
31
32
33
# File 'lib/rspec/swag/example_group_helpers.rb', line 29

def description(value = nil)
  return super() if value.nil?

  [:operation][:description] = value
end

#example(mime, name, value, summary = nil, description = nil) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rspec/swag/example_group_helpers.rb', line 93

def example(mime, name, value, summary = nil, description = nil)
  # Todo - move initialization of metadata somewhere else.
  [:response][:content] = {} if [:response][:content].blank?

  if [:response][:content][mime].blank?
    [:response][:content][mime] = {}
    [:response][:content][mime][:examples] = {}
  end

  example_object = {
    value: value,
    summary: summary,
    description: description
  }.select { |_, v| v.present? }
  # TODO, issue a warning if example is being overridden with the same key
  [:response][:content][mime][:examples].merge!(
    { name.to_sym => example_object }
  )
end

#examples(examples = nil) ⇒ Object

NOTE: Similar to ‘description’, ‘examples’ need to handle the case when being invoked with no params to avoid overriding ‘examples’ method of rspec-core ExampleGroup



84
85
86
87
88
89
90
91
# File 'lib/rspec/swag/example_group_helpers.rb', line 84

def examples(examples = nil)
  return super() if examples.nil?

  # should we add a deprecation warning?
  examples.each_with_index do |(mime, example_object), index|
    example(mime, "example_#{index}", example_object)
  end
end

#header(name, attributes) ⇒ Object



75
76
77
78
79
# File 'lib/rspec/swag/example_group_helpers.rb', line 75

def header(name, attributes)
  [:response][:headers] ||= {}

  [:response][:headers][name] = attributes
end

#parameter(attributes) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rspec/swag/example_group_helpers.rb', line 42

def parameter(attributes)
  attributes[:required] = true if attributes[:in] && attributes[:in].to_sym == :path

  if .key?(:operation)
    [:operation][:parameters] ||= []
    [:operation][:parameters] << attributes
  else
    [:path_item][:parameters] ||= []
    [:path_item][:parameters] << attributes
  end
end

#path(template, metadata = {}, &block) ⇒ Object



8
9
10
11
# File 'lib/rspec/swag/example_group_helpers.rb', line 8

def path(template,  = {}, &block)
  [:path_item] = { template: template }
  describe(template, , &block)
end

#request_body_example(value:, summary: nil, name: nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rspec/swag/example_group_helpers.rb', line 54

def request_body_example(value:, summary: nil, name: nil)
  return unless .key?(:operation)

  [:operation][:request_examples] ||= []
  example = { value: value }
  example[:summary] = summary if summary
  # We need the examples to have a unique name for a set of examples,
  # so just make the name the length if one isn't provided.
  example[:name] = name || [:operation][:request_examples].length
  [:operation][:request_examples] << example
end

#response(code, description, metadata = {}, &block) ⇒ Object



66
67
68
69
# File 'lib/rspec/swag/example_group_helpers.rb', line 66

def response(code, description,  = {}, &block)
  [:response] = { code: code, description: description }
  context(description, , &block)
end

#run_test!(description = nil, *args, **options, &block) ⇒ void

This method returns an undefined value.

Perform request and assert response matches swagger definitions

Parameters:

  • description (String) (defaults to: nil)

    description of the test

  • args (Array)

    arguments to pass to the ‘it` method

  • options (Hash)

    options to pass to the ‘it` method

  • &block (Proc)

    you can make additional assertions within that block



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/rspec/swag/example_group_helpers.rb', line 121

def run_test!(description = nil, *args, **options, &block)
  # swagger metadata value defaults to true
  options[:swagger] = true unless options.key?(:swagger)

  description ||= "returns a #{[:response][:code]} response"

  before do |example|
    submit_request(example.)
  end

  it description, *args, **options do |example|
    (example., &block)
    example.instance_exec(last_response, &block) if block_given?
  end
end

#schema(value) ⇒ Object



71
72
73
# File 'lib/rspec/swag/example_group_helpers.rb', line 71

def schema(value)
  [:response][:schema] = value
end