Rspec redirect_to check: back

How do you test redirect_to :back in rspec?

I get

ActionController::RedirectBackError :
HTTP_REFERER was not set in the request for this action, so redirect_to :back could not be called successfully. If this is a test, be sure to specify request.env["HTTP_REFERER"] .

How do I configure HTTP_REFERER in my test?

+57
redirect ruby-on-rails rspec rspec2
May 18 '11 at 6:00 a.m.
source share
6 answers

Using RSpec, you can set the referent to the before block. When I tried to set the referent directly in the test, it did not seem to work regardless of where I placed it, but this is a trick before the block.

 describe BackController < ApplicationController do before(:each) do request.env["HTTP_REFERER"] = "where_i_came_from" end describe "GET /goback" do it "redirects back to the referring page" do get 'goback' response.should redirect_to "where_i_came_from" end end end 
+104
Jul 18 '11 at 7:28 a.m.
source share

From rails guides when querying a query with a new query style:

 describe BackController < ApplicationController do describe "GET /goback" do it "redirects back to the referring page" do get :show, params: { id: 12 }, headers: { "HTTP_REFERER" => "http://example.com/home" } expect(response).to redirect_to("http://example.com/home") end end end 
+2
Oct 21 '16 at 18:19
source share

As for testing: backlinks in integration tests, Iโ€™m first on the dead page, which, I think, is unlikely to ever be used as a link, and then on the page I'm testing. So my code is as follows

  before(:each) do visit deadend_path visit testpage_path end it "testpage Page should have a Back button going :back" do response.should have_selector("a",:href => deadend_path, :content => "Back") end 

However, this has a drawback which, if the link really refers to deadend_path, then the test will fail by mistake.

+1
May 23 '12 at 23:42
source share

IMHO the accepted answer is a bit hacked. A better alternative would be to set HTTP_REFERER to the actual URL in your application, and then expect a redirect back:

 describe BackController, type: :controller do before(:each) do request.env['HTTP_REFERER'] = root_url end it 'redirects back' do get :whatever response.should redirect_to :back end end 
  • Redirecting to a random string constant seems to work randomly
  • You use the built-in rspec function to express exactly what you want.
  • You do not enter or repeat the values โ€‹โ€‹of the magic string



For newer versions of rspec, you can use expectations instead:

 expect(response).to redirect_to :back 
0
Apr 18 '16 at 2:51 on
source share

If someone stumbles upon this, and they use request specifications, you need to explicitly specify the headers in the request that you make. The format of the test request depends on which version of RSpec you are using, and if you can use keyword arguments instead of positional arguments.

 let(:headers){ { "HTTP_REFERER" => "/widgets" } } it "redirects back to widgets" do post "/widgets", params: {}, headers: headers # keyword (better) post "/widgets", {}, headers # positional expect(response).to redirect_to(widgets_path) end 

https://relishapp.com/rspec/rspec-rails/docs/request-specs/request-spec

0
Jul 20 '17 at 6:32
source share
 request.env['HTTP_REFERER'] = '/your_referring_url' 
-one
Jun 07 '11 at 18:12
source share



All Articles