【问题标题】:Cucumber default_url_options[:host] everytime "www.example.com" even if specified in environtemnts/test.rb黄瓜 default_url_options[:host] 每次“www.example.com”,即使在 environtemnts/test.rb 中指定
【发布时间】:2009-07-16 14:22:51
【问题描述】:

我在我的环境/test.rb 中指定了 default_url_options

config.action_mailer.default_url_options = { :host => "www.xyu.at" }

这很好,在我的黄瓜故事中,我测试用户注册, 用户激活链接正确生成

invitation_activation_url(1)
=> "www.xyu.at/signup/1231hj23jh23"

但是,当我尝试使用 features/steps/user_steps.rb 中的以下代码(使用来自http://github.com/bmabey/email-spec/tree/master 的 email-rspec)按照电子邮件中提供的链接进行操作时:

When /^I follow the invitation link$/ do
  When 'I follow "'+invitation_activation_url(1) + '" in the email'
end

这里的 url 是使用默认主机创建的:

invitation_activation_url(1)
=> "www.example.com/signup/1231hj23jh23"

有人可以帮助我吗?我不明白我做错了什么......

谢谢!

编辑:

好像和方法有关

current_url

但我不知道它来自哪里..?

编辑:

我在 features/support/env.rb 中指定了正确的环境

ENV["RAILS_ENV"] ||= "test"

编辑:

我的临时解决方案是,正如 edbond 所说,

invitation_activation_url(1, :host => "www.xyz.at")
  => "www.xyz.at/signup/1231hj23jh23"

但我不想以这种方式显式命名域 (我已经在我的环境/test.rb 文件中指定了它 - 这样它就不会干了)

【问题讨论】:

    标签: ruby-on-rails ruby routing url-rewriting


    【解决方案1】:

    在您的网址中使用 :host 选项。

    invitation_activation_url(1, :host => "www.xyz.at")
      => "www.xyz.at/signup/1231hj23jh23"
    

    编辑:

    您可以解析电子邮件正文并获取链接

      mail = YourMailer.deliveries.last
      email_html = Nokogiri::HTML mail.body.to_s
      approve_link = email_html.at_css("a")["href"]
    

    【讨论】:

    • 是的,这就是我的临时解决方案。但我不想以这种方式显式地命名域(-> 重复,因为我之前配置过 environment/test.rb!)
    • 正如 Lichtamberg 所说,如果您需要一次调用路由助手来工作,这是一种一次性的解决方法。但这不是一个通用的解决方案,尝试为任何复杂的应用程序实现它都是麻烦且不好的做法。
    【解决方案2】:

    我知道自从这篇文章发布以来已经有好几年了。但我遇到了这个问题,我花了几个小时来破译,直到我弄明白了。

    你应该改用

    When /^I follow the invitation link$/ do
      When 'I follow "'+invitation_activation_path(1) + '" in the email'
    end
    

    _url 生成 URL 路径;而 _path 生成 URI 路径。 web_steps.rb 使用 URI 来确定它用来计算主机的 current_url。

    来自http://api.rubyonrails.org/classes/ActionDispatch/Routing.html

    可以通过传递 :as 选项来命名路由,这样可以轻松 在您的源代码中引用完整 URL 的 name_of_route_url 和 URI 路径的 name_of_route_path。

    来自 web_steps.rb

    Then /^(?:|I )should be on (.+)$/ do |page_name|                                                                                                                                     |  #     end                                                                                                                                                                           
      current_path = URI.parse(current_url).path                                                                                                                                         |  #                                                                                                                                                                                   
      if current_path.respond_to? :should                                                                                                                                                |  #     collection do                                                                                                                                                                 
        current_path.should == path_to(page_name)                                                                                                                                        |  #       get 'sold'                                                                                                                                                                  
      else                                                                                                                                                                               |  #     end                                                                                                                                                                           
        assert_equal path_to(page_name), current_path                                                                                                                                    |  #   end                                                                                                                                                                             
      end                                                                                                                                                                                |
    end 
    

    【讨论】:

      【解决方案3】:

      您说您编辑了 config/environments/test.rb。您确定您的 Cucumber 功能实际上是在“测试”环境中执行的吗?

      我最近将 Cucumber 添加到我正在处理的项目中,它似乎将自己设置为默认使用“黄瓜”环境。

      在我的项目的 features/support/env.rb 中有这个:

      ENV["RAILS_ENV"] ||= "cucumber"
      

      所以如果你的项目类似,那么你也需要自定义 config/environments/cucumber.rb。

      【讨论】:

      • 是的,我已经把它改成了:ENV["RAILS_ENV"] ||= "test"
      【解决方案4】:

      我对 Cucumber 不是很熟悉,所以我不能肯定地说你必须在哪里应用这个修复程序。但问题是 default_url_options 未设置在您尝试生成 url 的另一个地方...

      所以我的建议是首先找出错误 url 是在什么上下文中生成的。在它之前或之后,只需输出 self.class。这就是你必须修补的课程。例如,假设打印了“ClassName”。

      当你拥有它时,在你的 config/environments/test.rb 中,只需添加属性访问器,然后将其设置为你想要的:

      class ClassName
        cattr_accessor :default_url_options
        # or mattr_ if it's a module
      end
      

      然后将其设置为与您的 actionmailer 相同的方式

      ClassName.default_url_options = { :host => "www.xyu.at" }
      

      当您想在模型或其他深奥的地方生成 url 时,整个过程也很有用(然后您还需要包含 ActionController::UrlWriter)。

      【讨论】:

      • 会不会 - 这是一个 webrat 错误?
      【解决方案5】:

      一种解决方案(基于信息here)是有一个类似的步骤定义

      Given /^the domain "(.+?)"$/ do |domain|
        host! domain
      end
      

      并像使用它

      Given the domain "www.foo.com"
      

      在功能中。

      不过,我遇到了未遵循重定向的问题。我尝试申请this patch,但没有成功。

      我最终在 Cucumber 的 env.rb 文件中使用了一个非常简单的解决方法:

      # There is a bug in internal_redirect? when used with explicit (non-example.com) domains.
      # This is a simple workaround but may break if we start specing external redirects.
      # https://webrat.lighthouseapp.com/projects/10503/tickets/140-current_url-should-be-fully-qualified
      class Webrat::Session
        alias internal_redirect? redirect?
      end
      

      正如评论中提到的,这当然可能会因外部重定向而中断,但我们没有。

      【讨论】:

        猜你喜欢
        • 2023-03-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-13
        • 1970-01-01
        相关资源
        最近更新 更多