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

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

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

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

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

テスト駆動開発

テスト駆動開発は、 プログラム開発手法の一種で、 プログラムに必要な各機能をテストとして書き、 そのテストが動作する必要最低限な実装を行い コードを洗練させる、といったサイクルを繰り返す手法の事です。

メール

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

Q&A

解決済

1回答

1983閲覧

rails turorial 11章の11.2.2 送信メールのテストを通したい。

BadXO

総合スコア6

Ruby on Rails 5

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

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

テスト駆動開発

テスト駆動開発は、 プログラム開発手法の一種で、 プログラムに必要な各機能をテストとして書き、 そのテストが動作する必要最低限な実装を行い コードを洗練させる、といったサイクルを繰り返す手法の事です。

メール

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

1グッド

0クリップ

投稿2019/10/21 04:34

前提・実現したいこと

現在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には引数は与えられていると思うのですがなぜこのようなエラーが出ているのでしょうか?
雑な質問で申し訳ございませんが、解答のほどよろしくお願い致します。

jun_nanika👍を押しています

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

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

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

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

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

guest

回答1

0

自己解決

テストコードを間違えていました。書き換えたら通りました。
正しくは以下のものです。

ruby

1require 'test_helper' 2 3class UserMailerTest < ActionMailer::TestCase 4 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

投稿2019/10/21 05:04

BadXO

総合スコア6

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問