Rails,测试邮件地址

Kon*_*rad 2 host rspec ruby-on-rails mailer capybara

我的应用程序发送包含绝对网址的电子邮件。

我在 config/environment/development.rb 中设置主机

config.action_mailer.default_url_options = { :host => 'localhost:3000' }
Run Code Online (Sandbox Code Playgroud)

现在我想测试电子邮件是否包含有效的 url。使用正则表达式,我从电子邮件中取出完整的 url,并想使用 Capybara 函数访问这个地址。

mail = ActionMailer::Base.deliveries.last
address = mail.body.to_s[%r{http.+/edit}]
visit address;
Run Code Online (Sandbox Code Playgroud)

但是我不知道在 config/environment/test.rb 中应该设置什么主机

当我设置 localhost:3000 时,它会尝试连接到由rails server命令启动的本地服务器。

你有什么想法来解决这个问题吗?

小智 5

什么对我有用:

Capybara.server_port = 3005
Run Code Online (Sandbox Code Playgroud)

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

config.action_mailer.default_url_options = { :host => 'localhost:3005' }
Run Code Online (Sandbox Code Playgroud)