【问题标题】:How can I specify https protocol in routing spec with rspec?如何使用 rspec 在路由规范中指定 https 协议?
【发布时间】:2011-09-06 09:32:04
【问题描述】:

在我的路线文件中,我有:

resources :subscription, :only => [:show], :constraints => {:protocol => "https"}

我正在尝试为这条路线添加一个规范,如下所示:

it "recognizes and generates #show" do
  { :get => "/subscription", :protocol => 'https' }.should route_to(:controller => "subscriptions", :action => "show")
end

但是,规范仍然失败。 如果我删除 :protocol => 'https',规范也会失败:

动作控制器::路由错误: 没有路线匹配“/订阅”

【问题讨论】:

    标签: ruby-on-rails rspec rspec-rails specifications


    【解决方案1】:

    (未记录的?)解决方案是简单地包含一个完整的虚拟 url,如下所示:

    it "recognizes and generates #show" do
      { :get => "https://test.host/subscription" }.should route_to(:controller => "subscriptions", :action => "show")
    end
    

    我是从this ticketthis changeset 发现的。

    【讨论】:

    • 为了让它工作,我必须将约束指定为{ protocol => "https://" },而不是像人们期望的那样简单地"https"
    【解决方案2】:

    我不确定,但我认为路线应该声明为复数形式——请参阅Rails Routing from the Outside In。所以会是

    resources :subscriptions
    

    在规范中

    {:get => '/subscriptions', :protocol => 'https'}
    

    如果没有:protocol,请尝试。如果是这样,请跳过带有 HTTPS 的规范。这不应该在单元测试级别上进行测试,而应该在集成测试中进行。

    【讨论】:

    • 我将“订阅”用作单一资源。请参阅您提到的指南中的第 2.5 节。
    • 对不起,我没想到。我添加了另一个答案。
    【解决方案3】:

    如果你把spec改成这个,它会通过吗?:

    {:get => '/subscription'}.should_not route_to(:controller => …)
    

    这至少会让您确信 HTTP 没有被路由。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-23
      • 1970-01-01
      • 2015-04-19
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      相关资源
      最近更新 更多