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.



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

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
  [:openapi_api_contexts] ||= []
  [:openapi_api_contexts] << context
  DSL::MetadataStore.register(context)
  context
end

#consumes(*content_types) ⇒ Object



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

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

#description(value = nil) ⇒ Object



68
69
70
71
# File 'lib/openapi_ruby/adapters/rspec.rb', line 68

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

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



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

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

#openapi_schema(name) ⇒ Object



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

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

#parameter(attributes = {}) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/openapi_ruby/adapters/rspec.rb', line 54

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



30
31
32
33
34
35
36
37
38
39
# File 'lib/openapi_ruby/adapters/rspec.rb', line 30

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



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

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

#request_body(attributes = {}) ⇒ Object



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

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

#request_body_example(**kwargs) ⇒ Object



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

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

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



89
90
91
92
93
94
95
96
97
# File 'lib/openapi_ruby/adapters/rspec.rb', line 89

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



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/openapi_ruby/adapters/rspec.rb', line 107

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



99
100
101
# File 'lib/openapi_ruby/adapters/rspec.rb', line 99

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