【问题标题】:How do I set the port correctly in email links during a system test?如何在系统测试期间在电子邮件链接中正确设置端口?
【发布时间】: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


    【解决方案1】:

    邮件中使用的 URL 指定为:
    Rails.application.configure.action_mailer.default_url_options

    在config/environments/test.rb 中,当我第一次安装 Clearance 时,我已将我的端口设置为 3000:
    config.action_mailer.default_url_options = {host: "localhost:3000"}

    为了修复它,我首先尝试了specifying the port dynamically,但建议的方法实际上并没有奏效,似乎没有必要。删除端口号足以让我的系统测试通过:
    config.action_mailer.default_url_options = {host: "localhost"}

    正如 Thomas Walpole 所提到的,它起作用的原因是 Capybara.always_include_port 设置为 true。使用此设置,访问http://localhost/confirm_email/&lt;token&gt;(未指定端口)的尝试会自动重新路由到http://localhost:&lt;port&gt;/confirm_email/&lt;token&gt;。

    Capybara gem 中的 always_include_port 设置 defaults to false 但事实证明 Rails 在 starts up the System Test server 时将其设置为 true。

    【讨论】:

    • 您可能将Capybara.always_include_port 设置为true,这将导致visit 在url 中未包含特定端口时插入正在运行应用程序的端口
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-26
    • 1970-01-01
    • 1970-01-01
    • 2017-10-25
    • 2013-05-17
    • 2013-07-14
    • 1970-01-01
    相关资源
    最近更新 更多