1

Rails 3.2 で、ActionMailer の 1 つである InvitationMailer のテストを書いていますが、「受信者」メソッドが見つからないようです。

私のテストは次のようになります。

  describe "Invitation" do
    it "should send invitation email to user" do
    user = Factory :user

    email = InvitationMailer.invitation_email(user).deliver
    # Send the email, then test that it got queued
    assert !ActionMailer::Base.deliveries.empty?

    # Test the body of the sent email contains what we expect it to
    assert_equal [user.email], email.to
    assert_equal "You have been Invited!", email.subject

    end

私の InvitationMailer は次のようになります。

class InvitationMailer < ActionMailer::Base
  default from: "webmaster@myapp.com"

  def invitation_email(user)
    recipients  user.email
    from        "invitations@myapp.com"
    subject     "You have been Invited!"
    body        :user => user
  end

end

ただし、次のエラー メッセージが表示されます。

 Failure/Error: email = InvitationEmail.invitation_email(user).deliver
 NoMethodError:
   undefined method `recipients' for #<InvitationMailer:0x007fca0b41f7f8>

それが何であるかについて何か考えはありますか?

4

1 に答える 1

4

Rails Guide for ActionMailerの例を次に示します。

class UserMailer < ActionMailer::Base
  default :from => "notifications@example.com"

  def welcome_email(user)
    @user = user
    @url  = "http://example.com/login"
    mail(:to => user.email,
         :subject => "Welcome to My Awesome Site",
         :template_path => 'notifications',
         :template_name => 'another')
  end
end

コードをこのようにすると、解決が容易になる可能性があるため、最初に次のように書き直します。

class InvitationMailer < ActionMailer::Base
  default from: "webmaster@myapp.com"

  def hassle_email(user)
    @user = user
    mail(:to => user.email,
         :subject => "You have been Invited!")
  end
end

次に、:to、 、:subjectおよび@userオブジェクトが、他のビューと同様にメーラーの「ビュー」に渡されます。

recipientsを使用しているため、メールを複数のメール アドレスに送信しようとしていたのかどうかわかりませんでした。その場合、ActionMailer のドキュメントによると:

電子メールのリストを :to キーに設定することにより、1 通の電子メールで 1 人または複数の受信者に電子メールを送信できます (たとえば、すべての管理者に新しいサインアップを通知するため)。電子メールのリストは、電子メール アドレスの配列、またはアドレスをコンマで区切った単一の文字列にすることができます。

于 2012-05-07T01:27:08.360 に答える