Module: OpenapiRuby::Adapters::RSpec::ExampleGroupHelpers

Defined in:
lib/openapi_ruby/adapters/rspec.rb

Overview

Class-level DSL methods extended onto :openapi example groups. All methods are inherited by nested describe/context/it_behaves_like blocks. Data is stored in RSpec metadata which propagates to child groups.

Instance Method Summary collapse

Instance Method Details

#api_path(template, &block) ⇒ Object

Minitest-style DSL: define the schema at the top of the spec file, then write normal RSpec examples underneath using assert_api_response.



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/openapi_ruby/adapters/rspec.rb', line 21

def api_path(template, &block)
  schema_name = [:openapi_schema_name]
  context = DSL::Context.new(template, schema_name: schema_name)
  context.instance_eval(&block) if block
  # Replace rather than push: RSpec copies parent metadata into child
  # groups by reference, so mutating the array in place would leak this
  # declaration back up to the parent and sideways to its siblings.
  # Building a new one is what makes a nested describe an actual scope.
  [:openapi_api_contexts] = ([:openapi_api_contexts] || []) + [context]
  DSL::MetadataStore.register(context)
  context
end

#consumes(*content_types) ⇒ Object



77
78
79
# File 'lib/openapi_ruby/adapters/rspec.rb', line 77

def consumes(*content_types)
  [:openapi_operation]&.consumes(*content_types)
end

#description(value = nil) ⇒ Object



72
73
74
75
# File 'lib/openapi_ruby/adapters/rspec.rb', line 72

def description(value = nil)
  return super() if value.nil?
  [:openapi_operation]&.description(value)
end

#header(name, attributes = {}) ⇒ Object



107
108
109
# File 'lib/openapi_ruby/adapters/rspec.rb', line 107

def header(name, attributes = {})
  [:openapi_response]&.header(name, attributes)
end

#openapi_schema(name) ⇒ Object



15
16
17
# File 'lib/openapi_ruby/adapters/rspec.rb', line 15

def openapi_schema(name)
  [:openapi_schema_name] = name.to_sym
end

#parameter(attributes = {}) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/openapi_ruby/adapters/rspec.rb', line 58

def parameter(attributes = {})
  if [:openapi_operation]
    [:openapi_operation].parameter(attributes)
  elsif [:openapi_path_context]
    [:openapi_path_context].parameter(attributes)
  end
end

#path(template, &block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/openapi_ruby/adapters/rspec.rb', line 34

def path(template, &block)
  schema_name = [:openapi_schema_name]
  context = DSL::Context.new(template, schema_name: schema_name)

  describe template do
    [:openapi_path_context] = context
    instance_eval(&block) if block
    DSL::MetadataStore.register(context)
  end
end

#produces(*content_types) ⇒ Object



81
82
83
# File 'lib/openapi_ruby/adapters/rspec.rb', line 81

def produces(*content_types)
  [:openapi_operation]&.produces(*content_types)
end

#request_body(attributes = {}) ⇒ Object



85
86
87
# File 'lib/openapi_ruby/adapters/rspec.rb', line 85

def request_body(attributes = {})
  [:openapi_operation]&.request_body(attributes)
end

#request_body_example(**kwargs) ⇒ Object



89
90
91
# File 'lib/openapi_ruby/adapters/rspec.rb', line 89

def request_body_example(**kwargs)
  [:openapi_operation]&.request_body_example(**kwargs)
end

#response(status_code, description, hidden: false, &block) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/openapi_ruby/adapters/rspec.rb', line 93

def response(status_code, description, hidden: false, &block)
  operation = [:openapi_operation]
  response_ctx = operation.response(status_code, description, hidden: hidden)

  context "response #{status_code} #{description}" do
    [:openapi_response] = response_ctx
    instance_eval(&block) if block
  end
end

#run_test!(description = nil, &block) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/openapi_ruby/adapters/rspec.rb', line 111

def run_test!(description = nil, &block)
  response_ctx = [:openapi_response]

  before do |example|
    submit_openapi_request(example.)
  end

  it(description || "returns #{response_ctx.status_code}") do |example|
    assert_openapi_response(example.)
    instance_eval(&block) if block
  end
end

#schema(definition) ⇒ Object



103
104
105
# File 'lib/openapi_ruby/adapters/rspec.rb', line 103

def schema(definition)
  [:openapi_response]&.schema(definition)
end