Skip to content

Commit 690fb49

Browse files
committed
Support have_http_status with Rack::MockResponse
1 parent 591ef0b commit 690fb49

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

lib/rspec/rails/matchers/have_http_status.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ def as_test_response(obj)
4949
resp.request = ActionDispatch::Request.new({})
5050
end
5151
::ActionDispatch::TestResponse.from_response(obj)
52+
elsif obj.respond_to?(:status) && obj.respond_to?(:headers) && obj.respond_to?(:body)
53+
# Hack to support `Rack::MockResponse` without having to load
54+
# Rack or catch `NameError`s for the undefined constants
55+
resp = ::ActionDispatch::Response.new(obj.status, obj.headers, obj.body)
56+
resp.request = ::ActionDispatch::Request.new({})
57+
::ActionDispatch::TestResponse.from_response(obj)
5258
else
5359
raise TypeError, "Invalid response type: #{obj}"
5460
end

spec/rspec/rails/matchers/have_http_status_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ def create_response(opts = {})
3939
end
4040
end
4141

42+
context "given something that acts as a Rack::MockResponse" do
43+
it "returns true for a response with the same code" do
44+
response = instance_double(
45+
'::Rack::MockResponse',
46+
status: code,
47+
headers: {},
48+
body: ""
49+
)
50+
51+
expect(matcher.matches?(response)).to be(true)
52+
end
53+
end
54+
4255
it "returns false given another type" do
4356
response = Object.new
4457

0 commit comments

Comments
 (0)