Skip to content

Commit 85fe74b

Browse files
committed
Merge pull request rspec#1440 from AEgan/redirect-render-message
modify error message when redirected but expecting to render a template
2 parents 9ca9059 + c0e5376 commit 85fe74b

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

Changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
### 3.4.0 Development
22
[Full Changelog](http://github.com/rspec/rspec-rails/compare/v3.3.3...master)
33

4+
Enhancements:
5+
6+
* Improved the failure message for `have_rendered` matcher on a redirect
7+
response. (Alex Egan, #1440)
8+
49
Bug Fixes:
510

611
* Fix another load order issued which causes an undefined method `fixture_path` error

lib/rspec/rails/matchers/have_rendered.rb

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,37 @@ def initialize(scope, expected, message = nil)
99
@expected = Symbol === expected ? expected.to_s : expected
1010
@message = message
1111
@scope = scope
12+
@redirect_is = nil
1213
end
1314

1415
# @api private
1516
def matches?(*)
16-
match_unless_raises ActiveSupport::TestCase::Assertion do
17+
match_check = match_unless_raises ActiveSupport::TestCase::Assertion do
1718
@scope.assert_template expected, @message
1819
end
20+
check_redirect unless match_check
21+
match_check
22+
end
23+
24+
# Uses normalize_argument_to_redirection to find and format
25+
# the redirect location. normalize_argument_to_redirection is private
26+
# in ActionDispatch::Assertions::ResponseAssertions so we call it
27+
# here using #send. This will keep the error message format consistent
28+
# @api private
29+
def check_redirect
30+
response = @scope.response
31+
return unless response.respond_to?(:redirect?) && response.redirect?
32+
@redirect_is = @scope.send(:normalize_argument_to_redirection, response.location)
1933
end
2034

2135
# @api private
2236
def failure_message
23-
rescued_exception.message
37+
if @redirect_is
38+
rescued_exception.message[/.* but /] +
39+
"was a redirect to <#{@redirect_is}>"
40+
else
41+
rescued_exception.message
42+
end
2443
end
2544

2645
# @api private

spec/rspec/rails/matchers/have_rendered_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,24 @@ def assert_template(*); raise "oops"; end
8888
end.to raise_exception("oops")
8989
end
9090
end
91+
92+
context "when fails with a redirect" do
93+
let(:response) { ActionController::TestResponse.new(302) }
94+
def assert_template(*)
95+
message = "expecting <'template_name'> but rendering with <[]>"
96+
raise ActiveSupport::TestCase::Assertion.new(message)
97+
end
98+
def normalize_argument_to_redirection(response_redirect_location)
99+
"http://test.host/widgets/1"
100+
end
101+
it "gives informative error message" do
102+
response = ActionController::TestResponse.new(302)
103+
response.location = "http://test.host/widgets/1"
104+
expect do
105+
expect(response).to send(template_expectation, "template_name")
106+
end.to raise_exception("expecting <'template_name'> but was a redirect to <http://test.host/widgets/1>")
107+
end
108+
end
91109
end
92110
end
93111
end

0 commit comments

Comments
 (0)