はじめに
みなさん、こんにちは torihaziです
今回はタイトルのことをやっていきます。
現状
現在、Rspecにおいてリクエストスペックのテストを行っています。
deviseを使って認証を行っており、ユーザ登録のテストをする際、次のエラーが出ました。
ActionView::Template::Error:
Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true
実際に使っていたコードは下記です。
deviseを使用して登録処理を行っているので、登録後認証メールが行く仕様になっています。
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'Users', type: :request do
describe 'POST #create' do
context "when the parameters is valid" do
it "request successfully" do
user_params = FactoryBot.attributes_for(:user)
post user_registration_path, params: {user: user_params}
expect(response.status).to eq 302
end
end
end
end
結論
congit/environments/test.rbに次の記述をします。
Rails.application.configure do
~~~
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
~~~
end
{ host: ***}のところは正直 hogehoge@mail.comとかでも機能します。
先ほどのエラーは送り元がわかんないから認証メール送れないよ、という内容でした。
開発環境(development.rb)や本番環境(production.rb)は
設定していたのに テスト環境だからということで忘れていました。
終わりに
以上!
まだエラーが出ているのでその解決に入ります!