ruby-on-rails - Rails测试默认请求主机

标签 ruby-on-rails unit-testing rspec

我希望所有单元测试都使用www.test.host而不是默认的test.host

我尝试在ENV['HTTP_HOST']中设置config/environments/test.rb,但是没有得到。

我的目的是避免 Controller 测试中的重定向,在我的测试中检查response对象的输出为:

#<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

最佳答案

您可以通过在测试中设置request.host来实现。您可以通过将其添加到test_helper.rb中来全局地进行操作(无论如何,在Rails 3.1中,不确定以前的版本,但我认为这是相似的):

class ActionController::TestCase
  setup do
    request.host = "www.test.host"
  end
end

关于ruby-on-rails - Rails测试默认请求主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7869357/

相关文章:

unit-testing - 理论与测试 Nunit

unit-testing - 以下要求的输入案例是什么

unit-testing - Grails 2.1 单元测试命令对象 mockForConstraintsTests 不工作?

ruby - RSpec any_instance 返回 self

ruby-on-rails - 在 rspec : `formatted_backtrace' : undefined method `backtrace' 中运行所有项目规范时出错

ruby-on-rails - 为什么 raise_error 匹配器没有按预期工作?

ruby-on-rails - 如何在 RoR 中生成唯一的匿名用户名?

ruby-on-rails - Rails 拥有并隶属于许多协会

mysql - 如何在数据库表列中搜索多个字符串?

ruby-on-rails - postgres unaccent 函数 vs RoR 音译