ume

rails Missing host to link to! Please provide the :host parameter, set default_url_options[:host] エラー

#エラーの経緯 モデルのバリデーションのテストをしようとかきをuser_spec.rbに記載しました↓

it 'should be unique in name' do 
      @user.save
      user = FactoryBot.build(:user, name: 'ゼルダ')
      expect(user).to be_invalid
    end

rspecコマンドでテストを走らせた時に以下のエラーが出力されました。

   Failure/Error: <p><%= link_to 'Confirm my account', confirmation_url(@resource, confirmation_token: @token) %></p>
     
     ActionView::Template::Error:
       Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true

結論

↓のコードをconfig/enviroments/test.rbファイルの一番下に追加するとエラーが解決できました。

config.action_mailer.default_url_options = { host: "localhost", port: 3000 }