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.



65
66
67
# File 'lib/openapi_first/test.rb', line 65

def definitions
  @definitions
end

Class Method Details

.[](api) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/openapi_first/test.rb', line 81

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.



50
51
52
53
54
55
56
57
# File 'lib/openapi_first/test.rb', line 50

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



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/openapi_first/test.rb', line 67

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.



41
42
43
44
45
# File 'lib/openapi_first/test.rb', line 41

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

.setup {|setup| ... } ⇒ Object

Yields:

Raises:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/openapi_first/test.rb', line 22

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
  yield setup
  return unless 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