前提・実現したいこと
現在rails tutorialで勉強中です。
rails turorial 11章の11.2.2 送信メールのプレビューのテストを通したいです。
■■な機能を実装中に以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
テストに表示されているエラーです。
ERROR["test_account_activation", UserMailerTest, 0.3481637069999124] test_account_activation#UserMailerTest (0.35s) ArgumentError: ArgumentError: wrong number of arguments (given 0, expected 1) app/mailers/user_mailer.rb:8:in `account_activation' test/mailers/user_mailer_test.rb:6:in `block in <class:UserMailerTest>' 1/1: [===================================] 100% Time: 00:00:00, Time: 00:00:00 Finished in 0.35140s 1 tests, 0 assertions, 0 failures, 1 errors, 0 skips
app/mailers/user_mailer.rb:8:in `account_activation' このエラーコードから account_activationに引数が期待している値というエラーだと思いました。以下に関係のありそうなコードを添付します。
該当のソースコード
app/mailers/user_mailer
ruby
1class UserMailer < ApplicationMailer 2 3 # Subject can be set in your I18n file at config/locales/en.yml 4 # with the following lookup: 5 # 6 # en.user_mailer.account_activation.subject 7 # 8 def account_activation(user) 9 @user = user 10 mail to: user.email, subject: "Account activation" #subjectキーは件名 11 end 12 13 # Subject can be set in your I18n file at config/locales/en.yml 14 # with the following lookup: 15 # 16 # en.user_mailer.password_reset.subject 17 # 18 def password_reset 19 @greeting = "Hi" 20 21 mail to: "to@example.org" 22 end 23end 24
テストコードのです。
usesr_mailer_test.rb
ruby
1require 'test_helper' 2 3class UserMailerTest < ActionMailer::TestCase 4 test "account_activation" do 5 mail = UserMailer.account_activation 6 assert_equal "Account activation", mail.subject 7 assert_equal ["to@example.org"], mail.to 8 assert_equal ["from@example.com"], mail.from 9 assert_match user.name, mail.body.encoded 10 assert_match user.activation_token, mail.body.encoded 11 assert_match CGI.escape(user.email), mail.body.encoded 12 end 13 14 15end 16
user_mailer_preview.rb
ruby
1# Preview all emails at http://localhost:3000/rails/mailers/user_mailer 2class UserMailerPreview < ActionMailer::Preview 3 4 # Preview this email at http://localhost:3000/rails/mailers/user_mailer/account_activation 5 def account_activation 6 user = User.first 7 user.activation_token = User.new_token 8 UserMailer.account_activation(user) 9 end 10 11 # Preview this email at http://localhost:3000/rails/mailers/user_mailer/password_reset 12 def password_reset 13 UserMailer.password_reset 14 end 15 16end 17
config/environments/test.rb
Rails.application.configure do # Settings specified here will take precedence over those in config/application.rb. # The test environment is used exclusively to run your application's # test suite. You never need to work with it otherwise. Remember that # your test database is "scratch space" for the test suite and is wiped # and recreated between test runs. Don't rely on the data there! config.cache_classes = true # Do not eager load code on boot. This avoids loading your whole application # just for the purpose of running a single test. If you are using a tool that # preloads Rails for running tests, you may have to set it to true. config.eager_load = false # Configure public file server for tests with Cache-Control for performance. config.public_file_server.enabled = true config.public_file_server.headers = { 'Cache-Control' => "public, max-age=#{1.hour.seconds.to_i}" } # Show full error reports and disable caching. config.consider_all_requests_local = true config.action_controller.perform_caching = false # Raise exceptions instead of rendering exception templates. config.action_dispatch.show_exceptions = false # Disable request forgery protection in test environment. config.action_controller.allow_forgery_protection = false config.action_mailer.perform_caching = false # Tell Action Mailer not to deliver emails to the real world. # The :test delivery method accumulates sent emails in the # ActionMailer::Base.deliveries array. config.action_mailer.delivery_method = :test config.action_mailer.default_url_options = { host: 'example.com' } # Print deprecation notices to the stderr. config.active_support.deprecation = :stderr # Raises error for missing translations # config.action_view.raise_on_missing_translations = true end
上のコードを見るとaccount_activationには引数は与えられていると思うのですがなぜこのようなエラーが出ているのでしょうか?
雑な質問で申し訳ございませんが、解答のほどよろしくお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。