Skip to content

Commit

Permalink
Prepend openapi! in any case
Browse files Browse the repository at this point in the history
If you run tests with Minitest at themoment, but without the environment variable `OPENAPI`, you are greeted with an error message.

`undefined method `openapi!' for Api::V0::SomeControllerTest:Class (NoMethodError)`

This is because the current code adds the `openapi!` method only if `OPENAPI` environment variable is set.

This commit modifies the loading logic, always requiring the `minitest` hooks class to be loaded if `Minitest` constant is defined, to avoid issues with RSpec. Then, the `openapi?` and `openapi!` methods are always prepended to `Minitest::Test`, while the modified `run` method and the `after_run` hook are only activated when `OPENAPI` environment variable is set.
  • Loading branch information
andyundso committed Jul 26, 2023
1 parent 0c92c20 commit 2af5fd7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
5 changes: 4 additions & 1 deletion lib/rspec/openapi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
require 'rspec/openapi/schema_merger'
require 'rspec/openapi/schema_cleaner'

if ENV['OPENAPI']
if Object.const_defined?('Minitest')

Check notice

Code scanning / Rubocop

Favor modifier if/unless usage when you have a single-line body. Note

Style/IfUnlessModifier: Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||.
require 'rspec/openapi/minitest_hooks'
end

if ENV['OPENAPI'] && Object.const_defined?('RSpec')

Check notice

Code scanning / Rubocop

Favor modifier if/unless usage when you have a single-line body. Note

Style/IfUnlessModifier: Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||.
require 'rspec/openapi/rspec_hooks'
end

Expand Down
20 changes: 12 additions & 8 deletions lib/rspec/openapi/minitest_hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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?
Expand All @@ -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?
Expand Down

0 comments on commit 2af5fd7

Please sign in to comment.