1

我正在为我的一个 ActionMailer - InvitationMailer 在 Rails 3.2 上编写测试,但是,它似乎没有找到“收件人”方法。

我的测试如下所示:

  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

这是ActionMailer 的 Rails 指南中的一个示例:

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 键,可以在一封电子邮件中向一个或多个收件人发送电子邮件(例如,通知所有管理员新注册)。电子邮件列表可以是电子邮件地址数组,也可以是地址以逗号分隔的单个字符串。

于 2012-05-07T01:27:08.360 回答