Rspec-rails: Rails 6 will remove success? predicate in favor of successful?

Created on 9 Aug 2017  路  8Comments  路  Source: rspec/rspec-rails

While running against Rails 5.2.0.alpha, I encountered the following message when running my test suite:

.DEPRECATION WARNING: The success? predicate is deprecated and will be removed in Rails 6.0. Please use successful? as provided by Rack::Response::Helpers. (called from matches? at rspec/rails/matchers/have_http_status.rb:263)

The message is coming from https://github.com/rails/rails/pull/30104 (https://github.com/rails/rails/commit/af3500b18817a48d7ec83812a0d4869f3fe33b2f)

Most helpful comment

Good point. In the interim you could do expect(response).to have_http_status(200) or whatever successful response code you'd expect.

Perhaps a more adequate solution would be to make these Rack Helpers the new GenericStatus.valid_statuses whilst deprecating the old ones.

All 8 comments

Basically the whole GenericStatus class functionality needs to be deprecated and removed in lockstep with Rails 6.0.

Probably something like:

        def self.matcher_for_status(target)
          if GenericStatus.valid_statuses.include?(target)
            RSpec.deprecate(
              GenericStatus.valid_statuses.map { |c| "`:#{c}`" }.join(', '),
              replacement: "the Rack::Response::Helpers predicates as symbols",
            )
            GenericStatus.new(target)
          elsif Symbol === target
            SymbolicStatus.new(target)
          else
            NumericCode.new(target)
          end
        end

alternatively we could just leave as is since the Rails deprecation takes care of it.

In your case you're probably using something like expect(response).to have_http_status(:success) in your tests. To get rid of the deprecation just use expect(response).to have_http_status(:successful)

Thanks for the answer, just tried that and got the following error :

1) HomeController GET #show returns http success
     Failure/Error: expect(response).to have_http_status(:successful)

     ArgumentError:
       Invalid HTTP status: :successful
     # ./spec/controllers/home_controller_spec.rb:11:in `block (3 levels) in <top (required)>'

I thinks this is because SymbolicStatus matches against Rack::Utils::SYMBOL_TO_STATUS_CODE, which does not contain :successful.

Good point. In the interim you could do expect(response).to have_http_status(200) or whatever successful response code you'd expect.

Perhaps a more adequate solution would be to make these Rack Helpers the new GenericStatus.valid_statuses whilst deprecating the old ones.

I'm writing a PR for the more adequate solution.

However, I am not sure about redirect: This commit removed it in favor of Rack redirect one, but semantics are not exactly the same, redirection means [300..399] and redirect means [301, 302, 307, 308].

This is a breaking change already in Rails 5, so I think we should support both here, but without any deprecation warning.

Is this fixed by #1951?

Closed by #1951

Was this page helpful?
0 / 5 - 0 ratings