【问题标题】:How can I test the request object using a specific URL in application_helper_spec.rb?如何使用 application_helper_spec.rb 中的特定 URL 测试请求对象?
【发布时间】:2011-06-25 15:54:26
【问题描述】:

我在 application_helper.rb 中定义了一个方法,它根据当前请求返回一个规范的 URL。如何模拟或以其他方式为控制器指定完整的 URL?

# spec/helpers/application_helper_spec.rb
describe "#canonical_url" do
  it "should return a path to an asset that includes the asset_host" do
    # Given: "http://www.foo.com:80/asdf.asdf?asdf=asdf"
    helper.canonical_url().should eq("http://www.foo.com/asdf.asdf")
  end
end

# app/helpers/application_helper.rb
def canonical_url
  "#{request.protocol}#{request.host}#{(request.port == 80) ? "" : request.port_string}#{request.path}"
end

编辑:

最终,我想测试 canonical_url() 是否为一堆不同的 URL 返回正确的字符串,有些带有端口,有些 w/o,有些带有查询字符串,有些带有路径等。也许这有点矫枉过正,但这就是最终目标。我想明确地存根/模拟/无论初始 URL,然后在匹配器中明确设置期望。我希望能够在一个电话中做到这一点,即controller.request.url = 'http://www.foo.com:80/asdf.asdf?asdf=asdf' 或request = ActionController::TestRequest.new :url => 'http://www.foo.com:80/asdf.asdf?asdf=asdf',但到目前为止,我还没有找到一个允许我这样做的“钩子”。这就是我正在寻找的解决方案。 如何明确定义给定测试的请求 URL。

【问题讨论】:

  • PS:我真的不在乎我传入的 URL 是什么。它不需要是应用程序自己的 URL,因为 canonical_url 方法应该适用于任何给定的 URL。但是,我确实想在测试之前明确定义它,而不是依赖于 ActionController::TestRequest 的默认值。

标签: ruby-on-rails unit-testing rspec2 helpers


【解决方案1】:

我会做的:

helper.request.stub(:protocol).and_return("http://")
helper.request.stub(:host).and_return("www.foo.com")
helper.request.stub(:port).and_return(80)
helper.request.stub(:port_string).and_return(":80")
helper.request.stub(:path).and_return("/asdf.asdf")
helper.canonical_url.should eq("http://www.foo.com/asdf.asdf")

【讨论】:

  • 我尝试了一个变体,但是使用controller.request.mock(:protocol) { 'http://' } 并得到一个错误,说在请求对象上未定义模拟。我可以使用您的示例再次尝试,但我真的很想在每次测试运行之前将其设置为单个字符串。
  • 我正要抱怨这种方法根本不起作用,因为我想要剥离的查询字符串部分没有在任何地方指定。但理想情况下,这由 Rails 内部 IRL 决定,#path 无论如何都应该只返回不带查询字符串的路径。所以,你是对的。有时我跑过“那是框架的工作”这一行,却没有意识到。
  • Still...我想知道是否有办法一次性指定 URL。
  • 先验,无论如何我都无法实现这一目标。但我会记住这个问题
【解决方案2】:

造成这种混乱的最终原因在于 ActionPack:

  • ActionDispatch::TestRequest
  • ActionDispatch::Http::URL

例如如果你设置了一个端口(ActionDispatch::TestRequest)

def port=(number)
  @env['SERVER_PORT'] = number.to_i
end

例如然后你读它(ActionDispatch::Http::URL)

def raw_host_with_port
  if forwarded = env["HTTP_X_FORWARDED_HOST"]
    forwarded.split(/,\s?/).last
  else
    env['HTTP_HOST'] || "#{env['SERVER_NAME'] || env['SERVER_ADDR']}:#{env['SERVER_PORT']}"
  end
end

只有在您没有设置 SERVER_NAME、HTTP_X_FORWARDED_HOST 或 HTTP_HOST 时,设置 SERVER_PORT 才会生效。

我对端口设置的基本解决方法是将端口添加到主机 - 因为 request.port 通常不会执行您想要的操作。

例如设置端口

request.host = 'example.com:1234'

真正的答案是阅读 ActionPack 中的代码;这很简单。

【讨论】:

  • 感谢您的后续回答。下次我会尝试这种方法。
【解决方案3】:

参加这个聚会很晚,但发现它在谷歌上搜索了类似的东西。

怎么样:

allow_any_instance_of(ActionController::TestRequest).to receive(:host).and_return('www.fudge.com')

我很感激 allow_any_instance_of 有时会被人嫌弃,但这似乎可以完成工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-03
    相关资源
    最近更新 更多