質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

パス

パス(path)はファイルシステムの場所(階層)を明示したものです。

メール

メールは、コンピュータネットワークを利用し、 情報等を交換する手段のことです。

Q&A

解決済

2回答

626閲覧

Railsチュートリアルの11章のテストにパスしない

iseita

総合スコア5

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

パス

パス(path)はファイルシステムの場所(階層)を明示したものです。

メール

メールは、コンピュータネットワークを利用し、 情報等を交換する手段のことです。

0グッド

0クリップ

投稿2020/01/22 04:35

編集2020/01/22 06:07

前提・実現したいこと

Railsチュートリアルの第11章の11.2.3のタスクである「送信メールのテスト」に取り組んでいるのですが、テストにパスしません。お時間がありましたら、アドバイスをお願いいたします。

エラーメッセージ

ec2-user:~/environment/sample_app (account-activation) $ rails test:mailers Started with run options --seed 36811 ERROR["test_account_activation", UserMailerTest, 0.3552794820006966] test_account_activation#UserMailerTest (0.36s) ActionView::Template::Error: ActionView::Template::Error: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true app/views/user_mailer/account_activation.html.erb:9:in `_app_views_user_mailer_account_activation_html_erb___3881546861189490321_45800140' app/mailers/user_mailer.rb:5:in `account_activation' test/mailers/user_mailer_test.rb:8:in `block in <class:UserMailerTest>' 1/1: [===============================================================================================================================] 100% Time: 00:00:00, Time: 00:00:00 Finished in 0.35870s 1 tests, 0 assertions, 0 failures, 1 errors, 0 skips

app/views/user_mailer/account_activation.htmlのコード

rails

1 2 3<h1>Sample App</h1> 4 5<p>Hi <%= @user.name %>,</p> 6 7<p> 8Welcome to the Sample App! Click on the link below to activate your account: 9</p> 10 11<%= link_to "Activate", edit_account_activation_url(@user.activation_token, 12 email: @user.email) %>

app/mailers/user_mailer.rbのコード

rails

1 2class UserMailer < ApplicationMailer 3 4 def account_activation(user) 5 @user = user 6 mail to: user.email, subject: "Account activation" 7 end 8 9 def password_reset 10 @greeting = "Hi" 11 12 mail to: "to@example.org" 13 end 14end

test/mailers/user_mailer_test.rbのコード

rails

1 2require 'test_helper' 3 4class UserMailerTest < ActionMailer::TestCase 5 test "account_activation" do 6 user = users(:michael) 7 user.activation_token = User.new_token 8 mail = UserMailer.account_activation(user) 9 assert_equal "Account activation", mail.subject 10 assert_equal [user.email], mail.to 11 assert_equal ["noreply@example.com"], mail.from 12 assert_match user.name, mail.body.encoded 13 assert_match user.activation_token, mail.body.encoded 14 assert_match CGI.escape(user.email), mail.body.encoded 15 end 16end

関連ファイルのコードも一応載せておきます。

config/environments/test.rbのコード
下記のコードが app/mailers/application_mailer.rb のものになっていたため、修正いたしました。

rails

1Rails.application.configure do 2 # Settings specified here will take precedence over those in config/application.rb. 3 4 # The test environment is used exclusively to run your application's 5 # test suite. You never need to work with it otherwise. Remember that 6 # your test database is "scratch space" for the test suite and is wiped 7 # and recreated between test runs. Don't rely on the data there! 8 config.cache_classes = true 9 10 # Do not eager load code on boot. This avoids loading your whole application 11 # just for the purpose of running a single test. If you are using a tool that 12 # preloads Rails for running tests, you may have to set it to true. 13 config.eager_load = false 14 15 # Configure public file server for tests with Cache-Control for performance. 16 config.public_file_server.enabled = true 17 config.public_file_server.headers = { 18 'Cache-Control' => "public, max-age=#{1.hour.seconds.to_i}" 19 } 20 21 # Show full error reports and disable caching. 22 config.consider_all_requests_local = true 23 config.action_controller.perform_caching = false 24 25 # Raise exceptions instead of rendering exception templates. 26 config.action_dispatch.show_exceptions = false 27 28 # Disable request forgery protection in test environment. 29 config.action_controller.allow_forgery_protection = false 30 config.action_mailer.perform_caching = false 31 32 # Tell Action Mailer not to deliver emails to the real world. 33 # The :test delivery method accumulates sent emails in the 34 # ActionMailer::Base.deliveries array. 35 config.action_mailer.delivery_method = :test 36 37 38 # Print deprecation notices to the stderr. 39 config.active_support.deprecation = :stderr 40 41 # Raises error for missing translations 42 # config.action_view.raise_on_missing_translations = true 43end 44 45

test/mailers/previews/user_mailer_preview.rbのコード

rails

1 2# Preview all emails at http://localhost:3000/rails/mailers/user_mailer 3class UserMailerPreview < ActionMailer::Preview 4 5 # Preview this email at http://localhost:3000/rails/mailers/user_mailer/account_activation 6 def account_activation 7 user = User.first 8 user.activation_token = User.new_token 9 UserMailer.account_activation(user) 10 end 11 12 # Preview this email at http://localhost:3000/rails/mailers/user_mailer/password_reset 13 def password_reset 14 UserMailer.password_reset 15 end 16 17end 18

app/mailers/application_mailer.rbのコード

rails

1 2class ApplicationMailer < ActionMailer::Base 3 default from: "noreply@example.com" 4 layout 'mailer' 5end 6

追加のコードです
config/environments/development.rb のコード

rails

1 2Rails.application.configure do 3 # Settings specified here will take precedence over those in config/application.rb. 4 5 # In the development environment your application's code is reloaded on 6 # every request. This slows down response time but is perfect for development 7 # since you don't have to restart the web server when you make code changes. 8 config.cache_classes = false 9 10 # Do not eager load code on boot. 11 config.eager_load = false 12 13 # Show full error reports. 14 config.consider_all_requests_local = true 15 16 # Enable/disable caching. By default caching is disabled. 17 if Rails.root.join('tmp/caching-dev.txt').exist? 18 config.action_controller.perform_caching = true 19 20 config.cache_store = :memory_store 21 config.public_file_server.headers = { 22 'Cache-Control' => "public, max-age=#{2.days.seconds.to_i}" 23 } 24 else 25 config.action_controller.perform_caching = false 26 27 config.cache_store = :null_store 28 end 29 30 # Don't care if the mailer can't send. 31 config.action_mailer.raise_delivery_errors = true 32 config.action_mailer.delivery_method = :test 33 config.action_mailer.default_url_options = { host: 'example.com' } 34 35 config.action_mailer.perform_caching = false 36 37 # Print deprecation notices to the Rails logger. 38 config.active_support.deprecation = :log 39 40 # Raise an error on page load if there are pending migrations. 41 config.active_record.migration_error = :page_load 42 43 # Debug mode disables concatenation and preprocessing of assets. 44 # This option may cause significant delays in view rendering with a large 45 # number of complex assets. 46 config.assets.debug = true 47 48 # Suppress logger output for asset requests. 49 config.assets.quiet = true 50 51 # Raises error for missing translations 52 # config.action_view.raise_on_missing_translations = true 53 54 # Use an evented file watcher to asynchronously detect changes in source code, 55 # routes, locales, etc. This feature depends on the listen gem. 56 config.file_watcher = ActiveSupport::EventedFileUpdateChecker 57end 58

試したこと

エラーメッセージに書かれていたすべてのファイルを確認して、一応コピペしたのですがパスしませんでした。

補足情報(FW/ツールのバージョンなど)

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

Missing host to link to! Please provide the :host parameter, set default_url_options[:host],
とあります。 メールに貼る link先の host部がないのでエラーに成っています。

config/environments/test.rb の中で
class ApplicationMailer < ActionMailer::Base
して設定した経験がないのですが、
Rails.application.configure do でやるなら
`config.action_mailer.default_url_options =
{ host: 'www.example.com' }`
とやります。
testで実際にはメールを出さないのでしょうから何でも良いのではと

投稿2020/01/22 05:26

winterboum

総合スコア23347

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

iseita

2020/01/22 06:29

回答していただきありがとうございました! 質問の中のコードが間違っていたために、ややこしくなってしまい申し訳ありませんでした。 ご指摘していただいたことで間違いに気づくことができました。
guest

0

自己解決

原因は
私がconfig/environments/development.rb と config/environments/test.rb 混同していたことにありました。

① config/environments/development.rb のコードの中の下記の部分を修正しました。

(修正前)
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :test
config.action_mailer.default_url_options = { host: 'example.com' }

(修正後)
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :test
host = 'rails-tutorial-mhartl.c9users.io' # クラウドIDE
config.action_mailer.default_url_options = { host: host, protocol: 'https' }

② config/environments/test.rb のコードの中の下記の部分を修正しました。

(修正前)

 config.action_mailer.delivery_method = :test

(修正後)
config.action_mailer.delivery_method = :test
config.action_mailer.default_url_options = { host: 'example.com' }

※ 最後の config.action_mailer.default_url_options = { host: 'example.com' } を追加しました。

無事テストをパスできました!

投稿2020/01/22 06:24

iseita

総合スコア5

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問