Rspec 测试 redirect_to :back [英] Rspec testing redirect_to :back
问题描述
你如何在 rspec 中测试 redirect_to :back
?
How do you test redirect_to :back
in rspec?
我明白了
ActionController::RedirectBackError
:
在此操作的请求中未设置 HTTP_REFERER
,因此无法成功调用 redirect_to :back
.如果这是一个测试,请确保指定 request.env["HTTP_REFERER"]
.
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"]
.
如何在测试中设置 HTTP_REFERER
?
推荐答案
使用 RSpec,您可以在 before
块中设置引用.当我尝试在测试中直接设置引用者时,无论我把它放在哪里,它似乎都不起作用,但是 before 块可以解决问题.
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
这篇关于Rspec 测试 redirect_to :back的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!