From 5a71461273811284cff7a8cde4502f382a9946d8 Mon Sep 17 00:00:00 2001 From: Jon Rowe Date: Tue, 25 Jun 2024 16:57:55 +0100 Subject: [PATCH] Merge pull request #2771 from cbliard/support-rack-mock-response-with-have-http-status Support `have_http_status` with `Rack::MockResponse` --- lib/rspec/rails/matchers/have_http_status.rb | 2 +- spec/rspec/rails/matchers/have_http_status_spec.rb | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/rspec/rails/matchers/have_http_status.rb b/lib/rspec/rails/matchers/have_http_status.rb index 878ceadc7..cc1b178be 100644 --- a/lib/rspec/rails/matchers/have_http_status.rb +++ b/lib/rspec/rails/matchers/have_http_status.rb @@ -33,7 +33,7 @@ def self.matcher_for_status(target) # @param obj [Object] object to convert to a response # @return [ActionDispatch::TestResponse] def as_test_response(obj) - if ::ActionDispatch::Response === obj + if ::ActionDispatch::Response === obj || ::Rack::MockResponse === obj ::ActionDispatch::TestResponse.from_response(obj) elsif ::ActionDispatch::TestResponse === obj obj diff --git a/spec/rspec/rails/matchers/have_http_status_spec.rb b/spec/rspec/rails/matchers/have_http_status_spec.rb index 5d75690c7..82333c74c 100644 --- a/spec/rspec/rails/matchers/have_http_status_spec.rb +++ b/spec/rspec/rails/matchers/have_http_status_spec.rb @@ -16,6 +16,14 @@ def create_response(opts = {}) end end + context "given a Rack::MockResponse" do + it "returns true for a response with the same code" do + response = ::Rack::MockResponse.new(code, {}, "") + + expect(matcher.matches?(response)).to be(true) + end + end + context "given an ActionDispatch::TestResponse" do it "returns true for a response with the same code" do response = ::ActionDispatch::TestResponse.new(code).tap { |x|