I've seen a lot of questions about errors concerning action_mailer.default_url_options and default_url_options[:host] during system tests.
Below I share a good solution I found.
In the file test/application_system_test_case.rb, append the following inside the setup block:
setup do
host = Capybara.current_session.server.host
port = Capybara.current_session.server.port
Rails.application.config.action_mailer.default_url_options = { host: host, port: port }
Rails.application.routes.default_url_options[:host] = host
end
This will make action_mailer.default_url_options and routes.default_url_options equal to whatever host and ports the tests had generated.
I think it's a better solution than hardcoding the values inside the environment files.