Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepend openapi! in any case #128

Merged
merged 3 commits into from
Aug 15, 2023
Merged

Prepend openapi! in any case #128

merged 3 commits into from
Aug 15, 2023

Conversation

andyundso
Copy link
Contributor

@andyundso andyundso commented Jul 26, 2023

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.

lib/rspec/openapi.rb Fixed Show fixed Hide fixed
lib/rspec/openapi.rb Fixed Show fixed Hide fixed
@codecov
Copy link

codecov bot commented Jul 26, 2023

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
Copy link
Owner

exoego commented Aug 15, 2023

Thanks. Makes sense to me.

@exoego exoego merged commit 7a6fda3 into exoego:master Aug 15, 2023
14 checks passed
@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.

2 participants