diff --git a/lib/rspec/openapi.rb b/lib/rspec/openapi.rb index a6c4a9c6..46b87535 100644 --- a/lib/rspec/openapi.rb +++ b/lib/rspec/openapi.rb @@ -10,8 +10,11 @@ require 'rspec/openapi/schema_merger' require 'rspec/openapi/schema_cleaner' -if ENV['OPENAPI'] +if Object.const_defined?('Minitest') require 'rspec/openapi/minitest_hooks' +end + +if ENV['OPENAPI'] && Object.const_defined?('RSpec') require 'rspec/openapi/rspec_hooks' end diff --git a/lib/rspec/openapi/minitest_hooks.rb b/lib/rspec/openapi/minitest_hooks.rb index 30ce3aa0..73dda985 100644 --- a/lib/rspec/openapi/minitest_hooks.rb +++ b/lib/rspec/openapi/minitest_hooks.rb @@ -5,11 +5,7 @@ module RSpec::OpenAPI::Minitest Example = Struct.new(:context, :description, :metadata, :file_path) - module TestPatch - def self.prepended(base) - base.extend(ClassMethods) - end - + module RunPatch def run(*args) result = super if ENV['OPENAPI'] && self.class.openapi? @@ -22,6 +18,12 @@ def run(*args) end result end + end + + module ActivateOpenApiClassMethods + def self.prepended(base) + base.extend(ClassMethods) + end module ClassMethods def openapi? @@ -35,10 +37,12 @@ def openapi! end end -Minitest::Test.prepend RSpec::OpenAPI::Minitest::TestPatch +Minitest::Test.prepend RSpec::OpenAPI::Minitest::ActivateOpenApiClassMethods + +if ENV['OPENAPI'] + Minitest::Test.prepend RSpec::OpenAPI::Minitest::RunPatch -Minitest.after_run do - if ENV['OPENAPI'] + Minitest.after_run do result_recorder = RSpec::OpenAPI::ResultRecorder.new(RSpec::OpenAPI.path_records) result_recorder.record_results! puts result_record.error_message if result_recorder.errors?