-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Prevent "have_http_status" from using deprecated methods in Rails 5.2+ #1951
Conversation
You'll need to rebase this so you don't bring across a load of |
Thank you for prompting that, Jon. Agreed and done. The commit history looked terrible, but the diff was okay. With your prompting, I rebased, squashed, and force pushed. Now you have one commit to backport if you want it. My view of the migration strategy is:
With that, no-one has to port forty tests when they upgrade. (I can only guess the number of extant |
@@ -18,7 +18,12 @@ module HaveHttpStatus | |||
# @return response matcher instance | |||
def self.matcher_for_status(target) | |||
if GenericStatus.valid_statuses.include?(target) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than check valid statuses and then doing the mapping, this could do the mapping, then check valid statuses. status_method = GenericStatus.map_status(target); if (status_method) ...
Then GenericStatus encapsulates all of the logic. I'm willing to make that change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd still like to see this class done away with, and the lookup method defined depending on the version of rails. See below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good but I'd still like to see a refactor here.
Also we'd be releasing this in the next minor version as we class it an enhancement, rather than a patch.
We're also unlikely to drop the old functionality for some time. We like to work with as many rails versions as possible.
@@ -255,12 +260,11 @@ def initialize(type) | |||
@invalid_response = nil | |||
end | |||
|
|||
# @return [Boolean] `true` if Rack's associated numeric HTTP code matched | |||
# the `response` code | |||
# @return [Boolean] value of response status method |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to see the original doc restored, with maybe an additional " or the named response status matches"
@@ -18,7 +18,12 @@ module HaveHttpStatus | |||
# @return response matcher instance | |||
def self.matcher_for_status(target) | |||
if GenericStatus.valid_statuses.include?(target) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd still like to see this class done away with, and the lookup method defined depending on the version of rails. See below.
|
||
def check_expected_status(test_response, expected) | ||
test_response.send("#{expected}?") | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Basically this would become something like:
if if 5 < ::Rails::VERSION::MAJOR || (::Rails::VERSION::MAJOR == 5 && 2 <= ::Rails::VERSION::MINOR)
def check_expected_status(test_response, expected)
test_response.send("#{expected}?")
end
else
RESPONSE_METHODS = {
success: 'successful',
error: 'server_error',
missing: 'not_found'
}.freeze
def check_expected_status(test_response, expected)
test_response.send("#{RESPONSE_METHODS.fetch(response_symbol, response_symbol)}?")
end
end
@@ -327,6 +337,33 @@ def type_codes | |||
end | |||
end | |||
end | |||
|
|||
class GenericStatusWithLookup < GenericStatus |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And this would go away.
Document the newly added internal method, "response_method" Add the Rails 5.2 beta as a test environment for travis Avoid defining tests specific to rails 5.2+ when not relevant Conditionally define method lookup for Rails 5.2 deprecated response status methods Add context for Rails 5.2 http status deprecation tests Remove stray print statement left in have_http_status_spec
Jon: Thank you for the further guidance on the change that you would like to see. I'm afraid I went a little further, and made the change forward compatible as well as backward compatible. Allowing the new syntax for any version didn't seem right unless it worked for any version. The two variants failing on Travis are failing in build. |
[ | ||
:error, :success, :missing, | ||
:server_error, :successful, :not_found, | ||
:redirect |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Almost there! Can we just get some specs covering the usage of the extra methods, these should run on all versions of Rails to show they're being mapped correctly on older versions. Thank you so much for your patience!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for prompting those additional tests. What could go wrong? It turns out that the type codes came back empty in the messages for those new generic methods.
I couldn't resist drying up the tests with a shared_examples block. They were very repetitive.
to(negated_message) | ||
end | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The diff from here down looks terrible. All that happened is:
- the repeated code in this shared example was replaced with invocation of this shared example
- new tests using the shared example were added for :not_found, :server_error, and :successful
it "raises an ArgumentError" do | ||
expect{ have_http_status(nil) }.to raise_error ArgumentError | ||
end | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything from here on is actually new, not changed lines.
@JonRowe The two failing combinations are gem dependency errors from bundler. If you hate the refactoring, I'll redo it copy-paste. Let me know if you would like any further changes. |
Thanks for the effort getting this over the line! |
In my opinion, this calls for a release, since the deprecation warning is REALLY annoying when upgrading rails projects. I would suggest a minor version bump, since it's an added feature for Rails 5.2 and it doesn't break existing behaviour. |
/cc @samphippen
|
[skip ci]
I second @jesperronn's suggestion for a point release. Now that Rails 5.2 is out, it would be neat if there was a released version of rspec-rails that doesn't not trigger deprecation warnings. |
Any news on a release for this? |
This resolves the deprecation warning: > The success? predicate is deprecated and will be removed in Rails 6.0. Please use successful? as provided by Rack::Response::Helpers. There is a [fix for this that has been merged](rspec/rspec-rails#1951) for rspec-rails, but is [not yet released](https://github.com/rspec/rspec-rails/blob/678b313cf3cc3bdb2c59149ee3e290fd590460d1/Changelog.md). We could revert it back afterwards if we're worried, but we expect this to be a `200` response at the moment, not a `202` or anything so this is fine I think.
This resolves the deprecation warning: > The success? predicate is deprecated and will be removed in Rails 6.0. Please use successful? as provided by Rack::Response::Helpers. There is a [fix for this that has been merged](rspec/rspec-rails#1951) for rspec-rails, but is [not yet released](https://github.com/rspec/rspec-rails/blob/678b313cf3cc3bdb2c59149ee3e290fd590460d1/Changelog.md). We could revert it back afterwards if we're worried, but we expect this to be a `200` response at the moment, not a `202` or anything so this is fine I think.
@samphippen Sorry to bump but I would also appreciate a point release including this for our Rails 5.2 upgrade ❤️ |
Prevent "have_http_status" from using deprecated methods in Rails 5.2+
I would also really appreciate a point release with this fix. We're currently pointing to 3.8.pre via Git SHAs, but it would be much nicer to just pull this from Rubygems. |
[skip ci]
This alternative PR (to #1945) Provides a minimum change to solve the problem of response status methods deprecated in Rails 5.2, as raised by issue #1857
This does so without deprecating or changing any status matchers in RSpec, so a Rails 5.2 upgrade will be transparent, at least with respect to not causing deprecation warnings for the :success, :error, and :missing values of has_http_status.
A major or minor release that eliminates the version check will either require Rails 5.2 and map :success to the
successful
call, or require everyone to change their tests.