我正在撰寫一個系統測驗來確認整個注冊流程在 Rails 7 應用程式中作業(使用Clearance gem和電子郵件確認 SignInGuard)。
直到我“單擊”電子郵件中的確認鏈接(在用 Nokogiri 決議后)之前,測驗都運行良好。出于某種原因,電子郵件中的 URL 指向我的開發服務器(埠 3000),而不是指向測驗服務器(埠 49736、49757、49991,等等)。
我可以查找測驗服務器正在使用的當前埠(它每次運行都會改變)并替換 URL 的埠部分,但這似乎很hacky。我是否遺漏了一些明顯的東西或做錯了什么?
郵件中的網址: 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
uj5u.com熱心網友回復:
郵件程式中使用的 URL 由以下內容指定:
Rails.application.configure.action_mailer.default_url_options
在config/environments/test.rb我第一次安裝 Clearance 時,我已經將我的埠設定為 3000:
config.action_mailer.default_url_options = {host: "localhost:3000"}
為了解決這個問題,我首先嘗試動態指定埠,但建議的方法實際上并沒有奏效,而且似乎沒有必要。洗掉埠號足以讓我的系統測驗通過:
config.action_mailer.default_url_options = {host: "localhost"}
正如 Thomas Walpole 所提到的,這有效的原因Capybara.always_include_port是設定為 true。使用此設定,嘗試訪問http://localhost/confirm_email/<token>(未指定埠)將自動重新路由到http://localhost:<port>/confirm_email/<token>。
Capybara gem 中的always_include_port設定默認為 false,但事實證明 Rails 在啟動系統測驗服務器時將其設定為 true 。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/361745.html
上一篇:Rust反序列化JSON
下一篇:如何從字串中獲取專案的值?
