在Rails minitest中测试动作邮件正文中的超链接是否解析



如果rails ActionMailer主体中的超链接指向具有3xx或2xx响应的位置,是否有方法测试(在MiniTest中(?

如果没有,是否有一种方法可以测试视图助手方法输出的链接是否有效?

听起来你想要一个集成测试。使用MiniTest,您可以通过对该页面进行请求并查看返回的响应来轻松完成此操作。不过,我不会只相信2xx或3xx代码。是的,链接有效,但你想知道链接的预期操作发生了吗?

我用这样的东西作为密码重置链接:

require 'test_helper'
include Sorcery::TestHelpers::Rails::Integration
class UsersControllerTest < ActionDispatch::IntegrationTest
test "it should send a password reset" do
get new_password_reset_path(email: User.first.email)
assert_response :success
end
end

但我想知道,链接实际上产生了正确的页面。我的密码重置页面实际上产生了一个3xx的代码:redirect。因此,我断言:found响应已收到,然后按照重定向并检查确认请求发送密码电子邮件的消息(请注意,如果您在延迟的工作中这样做,并且没有设置为立即接收错误响应,则如果您的实际邮件程序可能不起作用(。

所以我做了这样的事情:

require 'test_helper'
include Sorcery::TestHelpers::Rails::Integration
class UsersControllerTest < ActionDispatch::IntegrationTest
test "it should send a password reset" do
get new_password_reset_path(email: User.first.email)
assert_response :found
follow_redirect!
assert_includes @response.body, 'If you have a valid account you will receive an email with instructions to reset your password.', 'Does not include expected message'
end
end

@response是一个很好的集成测试工具,无需借助于Capybara/Selenium设置。如果你只想测试链接是否产生了4xx代码以外的东西,你可以在任何包含你正在测试的链接的控制器下设置一个集成测试,就像上面的测试一样。对链接执行适用的GET/POST,并测试响应或@response.body的预期行为。

如上所述,这并不能证明你的邮递员实际上在发送电子邮件,除非你的邮递人员确定了网络服务器对链接请求的实际响应。除此之外,我还会设置一个实际的邮件测试。

最新更新