无论是否在environments/test.rb中指定,Cucumber default_url_options[:host]每次都是“www.example.com”。

5

我在environments/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"

但我不想以这种方式显式地命名域名(我已经在我的 environments/test.rb 文件中指定了它 - 这样就不会变得单调乏味)

5个回答

1

我知道这篇文章已经发布多年了...但是我也遇到了这个问题,花了我几个小时才弄明白。

你应该使用以下方法

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 和 URI 路径提供 name_of_route_url 和 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 

1
使用 ":host" 选项在您的 URL 中。
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"]

1
是的,那是我的临时解决方案。但我不想以这种明确的方式命名域名(-> 重复,因为我之前在environments/test.rb中配置了它!) - BvuRVKyUVlViVIc7
正如Lichtamberg所说,如果您需要单个调用路由助手来工作,则这是一种一次性解决方法。但这不是一个通用的解决方案,试图为任何复杂度的应用程序实现它将是繁琐和不良的做法。 - Raphael

0
一个解决方案(基于此处的信息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

正如评论中所提到的,这当然可能会在外部重定向时出现问题,但我们没有遇到过。


0
你说你编辑了config/environments/test.rb。你确定你的Cucumber功能确实在“test”环境中执行吗?
我最近将Cucumber添加到我正在开发的项目中,它似乎默认设置为使用“cucumber”环境。
在我的项目中的features/support/env.rb中有这个:
ENV["RAILS_ENV"] ||= "cucumber"

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


是的,我已经将其更改为:ENV["RAILS_ENV"] ||= "test" - BvuRVKyUVlViVIc7

0

我对Cucumber不是特别熟悉,所以无法确定您需要在哪里应用此修复。但问题在于,在您尝试生成URL的另一个地方未设置default_url_options...

因此,我的建议是首先找出故障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 的 bug 吗? - BvuRVKyUVlViVIc7

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接