【发布时间】:2021-11-12 10:29:12
【问题描述】:
我正在编写系统测试以确认整个注册流程在 Rails 7 应用程序中运行(使用 Clearance gem 和电子邮件确认 SignInGuard)。
直到我“单击”电子邮件中的确认链接(在使用 Nokogiri 对其进行解析之后)之前,测试工作正常。由于某种原因,电子邮件中的 URL 指向我的开发服务器(端口 3000),而不是指向测试服务器(端口 49736、49757、49991,等等)。
我可以查找测试服务器正在使用的当前端口(每次运行都会更改)并替换 URL 的端口部分,但这似乎很麻烦。我是否遗漏了一些明显的东西或做错了什么?
邮件中的网址:confirm_email_url(@user.email_confirmation_token)
来自rails routes的路线:
Prefix Verb URI Pattern Controller#Action
confirm_email GET /confirm_email/:token(.:format) email_confirmations#update
目前的系统测试:
require "application_system_test_case"
require "test_helper"
require "action_mailer/test_helper"
class UserSignUpFlowTest < ApplicationSystemTestCase
include ActionMailer::TestHelper
test "Sign up for a user account" do
time = Time.now
email = "test_user_#{time.to_s(:number)}@example.com"
password = "Password#{time.to_s(:number)}!"
# Sign up (sends confirmation email)
visit sign_up_url
fill_in "Email", with: email
fill_in "Password", with: password
assert_emails 1 do
click_on "Sign up"
sleep 1 # Not sure why this is required... Hotwire/Turbo glitch?
end
assert_selector "span", text: I18n.t("flashes.confirmation_pending")
# Confirm
last_email = ActionMailer::Base.deliveries.last
parsed_email = Nokogiri::HTML(last_email.body.decoded)
target_link = parsed_email.at("a:contains('#{I18n.t("clearance_mailer.confirm_email.link_text")}')")
visit target_link["href"]
# ^^^^^^^^^^^^^^^^^^^^^^^^^
# This is the bit that fails... The link in the email points to my dev server (port 3000) rather than
# the test server (port 49736, 49757, 49991, etc). I only figured this out when my dev server crashed
# and Selenium started choking on a "net::ERR_CONNECTION_REFUSED" error
assert_selector "span", text: I18n.t("flashes.email_confirmed")
end
end
编辑
目前我已经通过将visit target_link["href"] 替换为visit hacky_way_to_fix_incorrect_port(target_link["href"]) 来解决它:
private
def hacky_way_to_fix_incorrect_port(url)
uri = URI(url)
return "#{root_url}#{uri.path}"
end
【问题讨论】:
标签: ruby-on-rails testing capybara clearance