Module: OpenapiFirst::Test

Defined in:
lib/openapi_first/test.rb,
lib/openapi_first/test/methods.rb,
lib/openapi_first/test/coverage.rb,
lib/openapi_first/test/coverage/plan.rb,
lib/openapi_first/test/plain_helpers.rb,
lib/openapi_first/test/minitest_helpers.rb,
lib/openapi_first/test/coverage/route_task.rb,
lib/openapi_first/test/coverage/request_task.rb,
lib/openapi_first/test/coverage/response_task.rb,
lib/openapi_first/test/coverage/terminal_formatter.rb

Overview

Test integration

Defined Under Namespace

Modules: Coverage, Methods, MinitestHelpers, PlainHelpers Classes: AlreadyRegisteredError, NotRegisteredError, Setup

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.definitionsObject (readonly)

Returns the value of attribute definitions.



90
91
92
# File 'lib/openapi_first/test.rb', line 90

def definitions
  @definitions
end

Class Method Details

.[](api) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/openapi_first/test.rb', line 106

def [](api)
  definitions.fetch(api) do
    option = api == :default ? '' : ", as: #{api.inspect}"
    raise(NotRegisteredError,
          "API description '#{api.inspect}' not found." \
          "Please call OpenapiFirst::Test.register('myopenapi.yaml'#{option}) " \
          'once before running tests.')
  end
end

.app(app, spec: nil, api: :default) ⇒ Object

Returns the Rack app wrapped with silent request, response validation You can use this if you want to track coverage via Test::Coverage, but don’t want to use the middlewares or manual request, response validation.



75
76
77
78
79
80
81
82
# File 'lib/openapi_first/test.rb', line 75

def self.app(app, spec: nil, api: :default)
  spec ||= self[api]
  Rack::Builder.app do
    use OpenapiFirst::Middlewares::ResponseValidation, spec:, raise_error: false
    use OpenapiFirst::Middlewares::RequestValidation, spec:, raise_error: false, error_response: false
    run app
  end
end

.minitest?(base) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
13
# File 'lib/openapi_first/test.rb', line 9

def self.minitest?(base)
  base.include?(::Minitest::Assertions)
rescue NameError
  false
end

.register(path, as: :default) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/openapi_first/test.rb', line 92

def register(path, as: :default)
  if definitions.key?(:default)
    raise(
      AlreadyRegisteredError,
      "#{definitions[as].filepath.inspect} is already registered " \
      "as ':default' so you cannot register #{path.inspect} without " \
      'giving it a custom name. Please call register with a custom key like: ' \
      "OpenapiFirst::Test.register(#{path.inspect}, as: :my_other_api)"
    )
  end

  definitions[as] = OpenapiFirst.load(path)
end

.report_coverage(formatter: Coverage::TerminalFormatter) ⇒ Object

Print the coverage report

Parameters:

  • formatter (defaults to: Coverage::TerminalFormatter)

    A formatter to define the report.



66
67
68
69
70
# File 'lib/openapi_first/test.rb', line 66

def self.report_coverage(formatter: Coverage::TerminalFormatter, **)
  coverage_result = Coverage.result
  puts formatter.new(**).format(coverage_result)
  puts "The overal API validation coverage of this run is: #{coverage_result.coverage}%"
end

.setupObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/openapi_first/test.rb', line 43

def self.setup(&)
  unless block_given?
    raise ArgumentError, "Please provide a block to #{self.class}.setup to register you API descriptions"
  end

  Coverage.start
  setup = Setup.new(&)

  if definitions.empty?
    raise NotRegisteredError,
          'No API descriptions have been registered. ' \
          'Please register your API description via ' \
          "OpenapiFirst::Test.setup { |test| test.register('myopenapi.yaml') }"
  end

  @setup ||= at_exit do
    setup.handle_exit
  end
end