Skip to content

Prepend openapi! in any case - #128

Merged
exoego merged 3 commits into
exoego:masterfrom
andyundso:prepend-openapi-in-any-case
Aug 15, 2023
Merged

Prepend openapi! in any case#128
exoego merged 3 commits into
exoego:masterfrom
andyundso:prepend-openapi-in-any-case

Conversation

@andyundso

@andyundso andyundso commented Jul 26, 2023

Copy link
Copy Markdown
Contributor

If you run tests with Minitest at the moment, 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.

Comment thread lib/rspec/openapi.rb Fixed
Comment thread lib/rspec/openapi.rb Fixed
@codecov

codecov Bot commented Jul 26, 2023

Copy link
Copy Markdown

Codecov Report

Merging #128 (a37d3ec) into master (0c92c20) will increase coverage by 0.24%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #128      +/-   ##
==========================================
+ Coverage   96.63%   96.88%   +0.24%     
==========================================
  Files          14       14              
  Lines         416      417       +1     
==========================================
+ Hits          402      404       +2     
+ Misses         14       13       -1     
Files Changed Coverage Δ
lib/rspec/openapi.rb 100.00% <100.00%> (ø)
lib/rspec/openapi/minitest_hooks.rb 89.65% <100.00%> (+4.46%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

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.
The alternative to this approach would be to remove the check for `Object.const_defined?('Minitest')`, but I assume in environments where you only have RSpec, not Minitest (like when you use another framework than Rails), this would not work.
@andyundso
andyundso marked this pull request as ready for review July 26, 2023 13:03
@exoego

exoego commented Aug 15, 2023

Copy link
Copy Markdown
Owner

Thanks. Makes sense to me.

@exoego
exoego merged commit 7a6fda3 into exoego:master Aug 15, 2023
@andyundso
andyundso deleted the prepend-openapi-in-any-case branch October 16, 2023 04:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants