How do you test redirect_to :back
in rspec?
I get
ActionController::RedirectBackError
:
NoHTTP_REFERER
was set in the request to this action, soredirect_to :back
could not be called successfully. If this is a test, make sure to specifyrequest.env["HTTP_REFERER"]
.
How do I go about setting the HTTP_REFERER
in my test?
Using RSpec, you can set the referer in a before
block. When I tried to set the referer directly in the test, it didn't seem to work no matter where I put it, but the before block does the trick.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With