Ruby on rails 如何在Rails 5中使用Capybara和Minitest测试密码重置邮件程序
我正在使用Rails中的Clearance gem以及Capybara和Minitest,但我不知道如何测试密码重置邮件程序。我不想测试Clearance gem,它已经经过了很好的测试,但是我想要一个高级集成测试,以确保预期的用户体验不会中断,这包括mailer 以下是我无法完成的测试(/integration/password\u reset\u test.rb): 我可以通过从服务器日志中获取密码重置电子邮件链接进行手动测试,因此该功能工作正常 我在网上找到的所有示例都假设您使用的是Rspec,但Minitest没有。我也尝试过使用,但是没有小测试的例子,我也不能让它工作 供参考:Ruby on rails 如何在Rails 5中使用Capybara和Minitest测试密码重置邮件程序,ruby-on-rails,capybara,integration-testing,minitest,clearance,Ruby On Rails,Capybara,Integration Testing,Minitest,Clearance,我正在使用Rails中的Clearance gem以及Capybara和Minitest,但我不知道如何测试密码重置邮件程序。我不想测试Clearance gem,它已经经过了很好的测试,但是我想要一个高级集成测试,以确保预期的用户体验不会中断,这包括mailer 以下是我无法完成的测试(/integration/password\u reset\u test.rb): 我可以通过从服务器日志中获取密码重置电子邮件链接进行手动测试,因此该功能工作正常 我在网上找到的所有示例都假设您使用的是Rsp
对于你想做的事
水豚电子邮件
是一个不错的选择。要设置它,您可以将它包括在ActionDispatch::IntegrationTest
中,或者包括在需要它的单个测试类中(在当前情况下-PasswordResetTest
)。您很可能还需要将ActiveJob配置为在作业排队时执行作业,而不是延迟作业(否则电子邮件实际上不会发送)。一种方法是包括ActiveJob::TestHelper
,然后使用它提供的perform\u enqueued\u jobs
方法。这导致了一些类似于
require 'test_helper'
class PasswordResetTest < ActionDispatch::IntegrationTest
include Capybara::Email::DSL
include ActiveJob::TestHelper
def setup
clear_emails
@user = create(:user)
end
test "User can reset their password" do
perform_enqueued_jobs do
visit "/login"
assert_current_path("/login")
click_link "Forgot password?"
assert_current_path("/passwords/new")
assert_selector "h1", text: "Reset your password"
fill_in("Email", :with => @user.email)
click_button "Reset your password"
assert_current_path("/passwords")
assert_selector "h1", text: "Your password reset email is on the way."
end
open_email(@user.email)
assert_content(current_email, 'blah blah')
current_email.click_link('Reset Password')
assert_current_path(reset_password_path)
... # fill out form with new password, etc.
end
end
等等。非常感谢!这里有很多有用的提示。我丢失了
include ActiveJob::TestHelper
和perform\u enqueued\u jobs
,因为capybara email
工作得很好。关于硬编码路径名,我认为您应该测试浏览器中的URL是否正常工作。我认为url是用户界面的一部分,因此测试/login
是否是预期的url对我来说是有意义的,因为Clearance使用登录路径
,但我希望按钮名称和url使用登录
。这是一个糟糕的方法吗?它之所以糟糕,是因为它使测试变得脆弱吗?@leemcally它导致了脆弱的测试,除非它是一个url,否则用户必须记住,我认为它也不是UI的一部分(Safari默认情况下实际上隐藏了url的路径部分)。如果你真的想测试特定的路径或者对特定的路径有要求(例如一个发布的API),我会添加一个或多个测试,这些测试只是一堆assert sign\u in\u path='/login'
等。所以它们都在一个地方被检查,然后在测试中使用帮助程序。这样,如果你需要将“/注册”改为“/加入我们”,这只是你必须改变的一个测试。这很有意义
assert_equal 1, ActionMailer::Base.deliveries.size
require 'test_helper'
class PasswordResetTest < ActionDispatch::IntegrationTest
include Capybara::Email::DSL
include ActiveJob::TestHelper
def setup
clear_emails
@user = create(:user)
end
test "User can reset their password" do
perform_enqueued_jobs do
visit "/login"
assert_current_path("/login")
click_link "Forgot password?"
assert_current_path("/passwords/new")
assert_selector "h1", text: "Reset your password"
fill_in("Email", :with => @user.email)
click_button "Reset your password"
assert_current_path("/passwords")
assert_selector "h1", text: "Your password reset email is on the way."
end
open_email(@user.email)
assert_content(current_email, 'blah blah')
current_email.click_link('Reset Password')
assert_current_path(reset_password_path)
... # fill out form with new password, etc.
end
end
assert_current_path(login_path)