Rails 4 ActionMailer การส่งอีเมลพร้อมชื่อไม่ทำงานตามที่คาดไว้

ฉันมีปัญหาในการทดสอบเมลของฉัน ดูเหมือนว่าจะกำหนดแอตทริบิวต์ [:to, :from, :reply_to] ด้วยอีเมลในรูปแบบที่อธิบายเป็น อีเมลพร้อมชื่อ ใช้งานไม่ได้

นี่คือตัวอย่างง่ายๆ

class MessageMailer < ActionMailer::Base
    def simple
      mail(
        from: "Aaron Test <[email protected]>",
        to: "Aaron Test <[email protected]>",
        reply_to: "Aaron Test <[email protected]>"
      )
    end
end

message_mailer_spec.rb

EXPECTED = "Aaron Test <[email protected]>"

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 <[email protected]>

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

    expected: "Aaron Test <[email protected]>"
        got: ["[email protected]"]

   (compared using ==)

ใครรู้วิธีกำหนด [to:, from:, Reply_to:] ในรูปแบบอีเมลพร้อมชื่อบ้าง

ฉันพลาดอะไรไปรึเปล่า?

มีวิธีการอื่นที่ใช้เก็บส่วนหัวของอีเมลที่ฉันสามารถทดสอบได้หรือไม่


person Aaron Renoir    schedule 22.09.2013    source แหล่งที่มา


คำตอบ (2)


โอเค ฉันได้รับมันโดยการทดสอบกับแฮชส่วนหัวของอีเมล

นี่คือวิธีทดสอบค่าของ 'from', 'reply_to' และ 'to' ใน rspec

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 <[email protected]>")
  end

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

  it "expect `to` to be formatted correctly" do
     expect( @email.header['To'].to_s ).to eq("Aaron Test <[email protected]>")
  end
end
person Aaron Renoir    schedule 01.10.2013
comment
ยังคงจำเป็นใน Rails5 แปลก. - person Sebastian Roth; 29.03.2017

* อัปเดต:

วงเล็บเหลี่ยมที่คุณได้รับแสดงว่าคุณกำลังรับอาร์เรย์ ดังนั้นคุณต้องแยกค่าออกจากอาร์เรย์เพื่อตรวจสอบความถูกต้อง เหตุผลก็คือ คุณสามารถมีผู้รับได้หลายคน ตัวอย่างเช่น วัตถุอาจส่งคืน: ["Aaron Test ", "Renoir "]


เท่าที่ฉันเข้าใจ คุณไม่จำเป็นต้องใช้วงเล็บเหลี่ยม และไม่จำเป็นต้องใส่เครื่องหมายคำพูดในสตริง

สิ่งเหล่านี้ควรใช้งานได้:

from: "Aaron Test <[email protected]>",
reply_to: "Aaron Test <[email protected]>"
person frandroid    schedule 23.09.2013
comment
ฉันเห็นด้วยนั่นควรจะได้ผล ฉันลอง 3 วิธีที่แตกต่างกันและผลการทดสอบก็เหมือนกัน ฉันเดาว่ามีวิธีการอื่นที่ฉันต้องทำการทดสอบ บางทีอาจแสดงมุมมองอีเมลและแยกวิเคราะห์ส่วนหัว - person Aaron Renoir; 24.09.2013
comment
ไม่ นั่นไม่ได้ผล ฉันโพสต์คำตอบที่แสดงวิธีทดสอบค่าส่วนหัว - person Aaron Renoir; 02.10.2013