Eth*_*han 87 ruby testing ruby-on-rails
我正在尝试测试控制器,我收到了这个错误.我理解错误,但不知道如何解决它.
test: on CREATE to :user with completely invalid email should respond with
redirect
(UsersControllerTest):ActionController::RedirectBackError:
No HTTP_REFERER was set in the request to this action,
so redirect_to :back could not be called successfully.
If this is a test, make sure to specify request.env["HTTP_REFERER"].
Run Code Online (Sandbox Code Playgroud)
指定它在哪里?我试过这个:
setup { post :create, { :user => { :email => 'invalid@abc' } },
{ 'referer' => '/sessions/new' } }
Run Code Online (Sandbox Code Playgroud)
但得到了同样的错误.
用什么确切地指定它?我想我希望它返回的视图的URI:
'/sessions/new'
Run Code Online (Sandbox Code Playgroud)
这是他们的意思吗?
好的,所以事实证明他们的意思是这样做:
setup do
@request.env['HTTP_REFERER'] = 'http://localhost:3000/sessions/new'
post :create, { :user => { :email => 'invalid@abc' } }, {}
end
Run Code Online (Sandbox Code Playgroud)
有人能告诉我记录在哪里吗?我想了解一下这些信息的背景.
如果域名不是"localhost:3000"怎么办?如果它是"localhost:3001"或者其他什么?有什么方法可以预料到这一点?
为什么这不起作用:
setup { post :create, { :user => { :email => 'invalid@abc' } },
{ 'referer' => '/sessions/new' } }
Run Code Online (Sandbox Code Playgroud)
Rails文档专门说明了你如何设置标题.
Jam*_*sen 90
他们的建议转化为以下内容:
setup do
@request.env['HTTP_REFERER'] = 'http://test.com/sessions/new'
post :create, { :user => { :email => 'invalid@abc' } }
end
Run Code Online (Sandbox Code Playgroud)
Fot*_*ios 45
接受的答案不适用于集成测试,因为该@request变量不存在.
根据RailsGuides,您可以将标头传递给帮助程序.
test "blah" do
get root_path, {}, {'HTTP_REFERER' => 'http://foo.com'}
...
end
Run Code Online (Sandbox Code Playgroud)
test "blah" do
get root_path, params: { id: 12 }, headers: { "HTTP_REFERER" => "http://foo.com" }
...
end
Run Code Online (Sandbox Code Playgroud)
在回答这个问题时:
为什么这不起作用:
setup { post :create, { :user => { :email => 'invalid@abc' } },
{ 'referer' => '/sessions/new' } }
Run Code Online (Sandbox Code Playgroud)
它不起作用,因为您链接到文档的Rails文档与您可能使用的文档不同.
你链接到ActionController::Integration:Session.我猜你正在编写一个功能测试(如果你使用的是Test :: Unit)或一个控制器测试(如果你正在使用Rspec).无论哪种方式,您可能正在使用ActionController::TestCase或其子类.反过来,它包括该模块ActionController::TestProcess.
ActionController::TestProcess提供了一个get具有不同参数比方法get通过提供的方法ActionController::Integration:Session.(烦人,嗯?)方法签名是这样的:
def get(action, parameters = nil, session = nil, flash = nil)
Run Code Online (Sandbox Code Playgroud)
遗憾的是,没有header参数.但至少设置@request.env['HTTP_REFERER']工作.
| 归档时间: |
|
| 查看次数: |
31524 次 |
| 最近记录: |