Rails,测试邮件 URL

发布于 2024-11-25 14:10:33 字数 539 浏览 8 评论 0原文

我的应用程序发送包含绝对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

如梦亦如幻 2024-12-02 14:10:33

对我有用的:(

Capybara.server_port = 3005

参见 https://github.com/vangberg/capybara/commit/5784f03d6aa87e63e759abda794a43738e4f320f

config.action_mailer.default_url_options = { :host => 'localhost:3005' }

What worked for me:

Capybara.server_port = 3005

(see https://github.com/vangberg/capybara/commit/5784f03d6aa87e63e759abda794a43738e4f320f)

and

config.action_mailer.default_url_options = { :host => 'localhost:3005' }
李白 2024-12-02 14:10:33

要在无法选择对 Capybara 端口进行硬编码(例如并行规格)时启用此功能,请将其放入您的 spec_helper.rb 中

 YourApp::Application.config.action_mailer.default_url_options[:host] = "localhost:#{Capybara.server_port}"

To enable this to work when hardcoding Capybara ports is not an option (eg. parallel specs), put this in your spec_helper.rb

 YourApp::Application.config.action_mailer.default_url_options[:host] = "localhost:#{Capybara.server_port}"
回忆凄美了谁 2024-12-02 14:10:33

为什么不直接根据正则表达式验证 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

如歌彻婉言 2024-12-02 14:10:33

嘿,这是可怕的代码片段,也许可以帮助你。我使用 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:)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文