1

我无法测试我的邮件程序。似乎用描述为email-with-name格式的电子邮件分配属性 [:to, :from, :reply_to]不起作用。

这是一个简单的例子。

class MessageMailer < ActionMailer::Base
    def simple
      mail(
        from: "Aaron Test <aaron@test.com>",
        to: "Aaron Test <aaron@test.com>",
        reply_to: "Aaron Test <aaron@test.com>"
      )
    end
end

message_mailer_spec.rb

EXPECTED = "Aaron Test <aaron@test.com>"

describe MessageMailer do
  before do
    @email = MessageMailer.simple
  end

  it "expect `from` to eq #{EXPECTED}" do
    expect( @email.from ).to eq(EXPECTED)
  end

  it "expect `to` to eq #{EXPECTED}" do
    expect( @email.to ).to eq(EXPECTED)
  end

  it "expect `reply_to` to eq #{EXPECTED}" do
    expect( @email.reply_to ).to eq(EXPECTED)
  end
end

测试结果都一样

1) MessageMailer expect `reply_to` to eq Aaron Test <aaron@test.com>

  Failure/Error: expect( @email.reply_to ).to eq(EXPECTED)

    expected: "Aaron Test <aaron@test.com>"
        got: ["aaron@test.com"]

   (compared using ==)

任何人都知道如何以 email-with-name 格式分配 [to:, from:, reply_to:] 吗?

我错过了什么吗?

是否有不同的方法来保存我可以测试的电子邮件标题?

4

2 回答 2

3

好的,我通过测试电子邮件的标题哈希得到它。

这是如何在 rspec 中测试 'from'、'reply_to' 和 'to' 的值。

describe MessageMailer do
  before do
    @email = MessageMailer.simple
  end

  it "expect `from` to be formatted correctly" do
     expect( @email.header['From'].to_s ).to eq("Aaron Test <aaron@test.com>")
  end

  it "expect `reply_to` to be formatted correctly" do
     expect( @email.header['Reply-To'].to_s ).to eq("Aaron Test <aaron@test.com>")
  end

  it "expect `to` to be formatted correctly" do
     expect( @email.header['To'].to_s ).to eq("Aaron Test <aaron@test.com>")
  end
end
于 2013-10-01T23:16:06.867 回答
0

*更新:

您得到的方括号表明您正在接收一个数组。所以你需要从数组中提取值来验证它们的有效性。这样做的原因是您可能有多个收件人,因此例如对象可能返回:[“Aaron Test”,“Renoir”]


据我了解,您不需要使用方括号,也不需要在字符串中包含引号。

这些应该工作:

from: "Aaron Test <aaron@test.com>",
reply_to: "Aaron Test <aaron@test.com>"
于 2013-09-23T18:08:18.767 回答