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
-
.definitions ⇒ Object
readonly
Returns the value of attribute definitions.
Class Method Summary collapse
- .[](api) ⇒ Object
-
.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.
- .minitest?(base) ⇒ Boolean
-
.register(path_or_definition, as: :default) ⇒ Object
Register an OpenAPI definition for testing.
-
.report_coverage(formatter: Coverage::TerminalFormatter) ⇒ Object
Print the coverage report.
-
.setup {|setup| ... } ⇒ Object
Sets up OpenAPI test coverage and OAD registration.
Class Attribute Details
.definitions ⇒ Object (readonly)
Returns the value of attribute definitions.
106 107 108 |
# File 'lib/openapi_first/test.rb', line 106 def definitions @definitions end |
Class Method Details
.[](api) ⇒ Object
127 128 129 130 131 132 133 134 135 |
# File 'lib/openapi_first/test.rb', line 127 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.
91 92 93 94 95 96 97 98 |
# File 'lib/openapi_first/test.rb', line 91 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
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_or_definition, as: :default) ⇒ Object
Register an OpenAPI definition for testing
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/openapi_first/test.rb', line 111 def register(path_or_definition, as: :default) if definitions.key?(as) && as == :default raise( AlreadyRegisteredError, "#{definitions[as].filepath.inspect} is already registered " \ "as ':default' so you cannot register #{path_or_definition.inspect} without " \ 'giving it a custom name. Please call register with a custom key like: ' \ "OpenapiFirst::Test.register(#{path_or_definition.inspect}, as: :my_other_api)" ) end definition = OpenapiFirst.load(path_or_definition) definitions[as] = definition definition end |
.report_coverage(formatter: Coverage::TerminalFormatter) ⇒ Object
Print the coverage report
83 84 85 86 |
# File 'lib/openapi_first/test.rb', line 83 def self.report_coverage(formatter: Coverage::TerminalFormatter, **) coverage_result = Coverage.result puts formatter.new(**).format(coverage_result) end |
.setup {|setup| ... } ⇒ Object
Sets up OpenAPI test coverage and OAD registration.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/openapi_first/test.rb', line 59 def self.setup(&) unless block_given? raise ArgumentError, "Please provide a block to #{self.class}.setup to register you API descriptions" end Coverage.install setup = Setup.new(&) Coverage.start(skip_response: setup.skip_response_coverage) 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 |