Class: OpenapiFirst::Test::Setup

Inherits:
Object
  • Object
show all
Defined in:
lib/openapi_first/test.rb

Overview

Helper class to setup tests

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Setup

Returns a new instance of Setup.

Yields:

  • (_self)

Yield Parameters:



17
18
19
20
21
22
23
# File 'lib/openapi_first/test.rb', line 17

def initialize
  @minimum_coverage = 0
  @coverage_formatter = Coverage::TerminalFormatter
  @coverage_formatter_options = {}
  @skip_response_coverage = nil
  yield self
end

Instance Attribute Details

#coverage_formatterObject

Returns the value of attribute coverage_formatter.



29
30
31
# File 'lib/openapi_first/test.rb', line 29

def coverage_formatter
  @coverage_formatter
end

#coverage_formatter_optionsObject

Returns the value of attribute coverage_formatter_options.



29
30
31
# File 'lib/openapi_first/test.rb', line 29

def coverage_formatter_options
  @coverage_formatter_options
end

#minimum_coverageObject

Returns the value of attribute minimum_coverage.



29
30
31
# File 'lib/openapi_first/test.rb', line 29

def minimum_coverage
  @minimum_coverage
end

Instance Method Details

#handle_exitObject

This called at_exit



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/openapi_first/test.rb', line 38

def handle_exit
  coverage = Coverage.result.coverage
  # :nocov:
  puts 'API Coverage did not detect any API requests for the registered API descriptions' if coverage.zero?
  if coverage.positive?
    Test.report_coverage(
      formatter: coverage_formatter,
      **coverage_formatter_options
    )
  end
  return unless minimum_coverage > coverage

  puts "API Coverage fails with exit 2, because API coverage of #{coverage}% " \
       "is below minimum of #{minimum_coverage}%!"
  exit 2
  # :nocov:
end

#register(oad, as: :default) ⇒ Object



25
26
27
# File 'lib/openapi_first/test.rb', line 25

def register(oad, as: :default)
  Test.register(oad, as:)
end

#skip_response_coverage(&block) ⇒ Object



31
32
33
34
35
# File 'lib/openapi_first/test.rb', line 31

def skip_response_coverage(&block)
  return @skip_response_coverage unless block_given?

  @skip_response_coverage = block
end