Rails,测试邮件 URL
我的应用程序发送包含绝对 URL 的电子邮件。
我在 config/environment/development.rb 中设置了主机
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
现在我想测试电子邮件是否包含有效的网址。使用正则表达式,我从电子邮件中取出完整的 URL,并希望使用 Capybara 函数访问该地址。
mail = ActionMailer::Base.deliveries.last
address = mail.body.to_s[%r{http.+/edit}]
visit address;
但我不知道应该在 config/environment/test.rb 中设置哪个主机
当我设置 localhost:3000 时,它会尝试连接到由 rails server
命令启动的本地服务器。
您有什么想法来解决这个问题吗?
My application sends E-mails containing absolute urls.
I set host in config/environment/development.rb
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
Now I want to test if Email contains valid url. Using regular expression I take out full url from E-mail and want to visit this address using Capybara function.
mail = ActionMailer::Base.deliveries.last
address = mail.body.to_s[%r{http.+/edit}]
visit address;
But I don't know what host should be set in config/environment/test.rb
When I set localhost:3000 it tries to connect to my local server started by rails server
command.
Do you have any ideas to solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对我有用的:(
参见 https://github.com/vangberg/capybara/commit/5784f03d6aa87e63e759abda794a43738e4f320f)
和
What worked for me:
(see https://github.com/vangberg/capybara/commit/5784f03d6aa87e63e759abda794a43738e4f320f)
and
要在无法选择对 Capybara 端口进行硬编码(例如并行规格)时启用此功能,请将其放入您的 spec_helper.rb 中
To enable this to work when hardcoding Capybara ports is not an option (eg. parallel specs), put this in your spec_helper.rb
为什么不直接根据正则表达式验证 URL,而不是访问 URL,而是确保它有效、编码正确、指向正确的控制器。
您可能不想与服务器实际交互,如果您不想与生产服务器交互,那么 localhost:3000 是一个不错的选择
Instead of visiting the URL why not just validate it against a RegEx, make sure that it is valid, encoded correctly, points to the right controller.
You probably don't want to actually interact with the server, and if you do not the production server so localhost:3000 is a goo option
嘿,这是可怕的代码片段,也许可以帮助你。我使用 email_spec 来简化规范和黄瓜场景中的电子邮件处理,并且它已经有一些帮助程序可以简化你的任务。在我的应用程序中,我遇到了更复杂的情况,因此我被迫编写自己的解析器。这是代码。享受:)
Hey here is scary code snippet that maybe could help you. I use email_spec to simplify working with email in specs and cucumber scenarios and it already has some helpers to simplify your task. In my application I have a little more complexer situation so I was forced to write my own parser. Here is the code. Enjoy:)