Rails 测试默认请求主机
我希望所有单元测试都使用 www.test.host
而不是默认的 test.host
。
我尝试在 config/environments/test.rb 中设置 ENV['HTTP_HOST'],但没有成功。
我的目的是避免控制器测试中的重定向,在我的测试中检查响应对象的输出是:
#<ActionController::TestResponse:0x000001059ed378, ..., @header={"Location"=>"http://www.test.host", ... , @status=301, @body=["<html><body>You are being <a href=\"http://www.test.host\">redirected</a>.</body></html>"], ... , "REQUEST_METHOD"=>"GET", "SERVER_NAME"=>"example.org", "SERVER_PORT"=>"80",... , "HTTP_HOST"=>"test.host", "REMOTE_ADDR"=>"0.0.0.0" ...>>
如果有影响,我正在使用 Rails3 和 RSPEC2
I would like all my unit tests to use www.test.host
instead of the default test.host
.
I tried setting ENV['HTTP_HOST']
in config/environments/test.rb
, but that didn't get it.
My purpose is to avoid a redirect in my controller test, the output of inspecting the response
object in my test is:
#<ActionController::TestResponse:0x000001059ed378, ..., @header={"Location"=>"http://www.test.host", ... , @status=301, @body=["<html><body>You are being <a href=\"http://www.test.host\">redirected</a>.</body></html>"], ... , "REQUEST_METHOD"=>"GET", "SERVER_NAME"=>"example.org", "SERVER_PORT"=>"80",... , "HTTP_HOST"=>"test.host", "REMOTE_ADDR"=>"0.0.0.0" ...>>
If it makes a difference, I'm using Rails3 and RSPEC2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过在测试中设置
request.host
来完成此操作。您可以通过将其添加到test_helper.rb
来全局执行此操作(无论如何,在 Rails 3.1 中,不确定以前的版本,但我认为它是相似的):You do it by setting
request.host
in your test. You can do it globally by adding this to yourtest_helper.rb
(in Rails 3.1 anyway, not sure about previous versions, but I think it's similar):