我在 Rails 中使用 Clearance gem 以及 Capybara 和 Minitest,但我不知道如何测试密码重置邮件。我不想测试已经经过良好测试的 Clearance gem,但我想要一个高级集成测试以确保不会破坏预期的用户体验,其中包括邮件程序。
这是我无法完成的测试(/integration/password_reset_test.rb):
require 'test_helper'
class PasswordResetTest < ActionDispatch::IntegrationTest
def setup
ActionMailer::Base.deliveries.clear
@user = create(:user)
end
test "User can reset their password" do
visit "/login"
assert page.current_path == "/login"
click_link "Forgot password?"
assert page.current_path == "/passwords/new"
assert_selector "h1", text: "Reset your password"
fill_in("Email", :with => @user.email)
click_button "Reset your password"
assert page.current_path == "/passwords"
assert_selector "h1", text: "Your password reset email is on the way."
# This is where I'm stuck
# Would like to test that the correct email was sent by checking
# basic content in the email and then simulate a user clicking
# the password reset link and then updating their password.
end
end
您如何实际测试邮件是否正确发送,有没有办法模拟水豚点击电子邮件中的密码重置链接,然后填写表格以重置密码?
我也试过这个,但是这条线失败了,所以我显然没有做对:
assert_equal 1, ActionMailer::Base.deliveries.size
我可以通过从服务器日志中获取密码重置电子邮件链接来手动测试,因此该功能可以正常工作。
我可以在网上找到的所有示例都假设您使用的是 Rspec,但对于 Minitest 则没有。我也尝试使用capybara-email gem,但没有 Minitest 示例,我也无法使其正常工作。
供参考: Gemfile test_helper.rb