2013-09-22 42 views
1

我無法測試我的郵件。它看起來以email-with-name格式的電子郵件分配屬性[:to,:from,:reply_to]不起作用。Rails 4 ActionMailer發送電子郵件名稱不按預期工作

這是一個簡單的例子。

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 ==) 

任何人都知道如何分配[從:, REPLY_TO聯繫::]在電子郵件,與 - 名稱格式?

我錯過了什麼嗎?

是否有不同的方法來保存我可以測試的電子郵件標題?

回答

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 <[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 
+0

Rails5中仍然需要。奇怪的。 –

0

*更新:

你得到的方括號表示您收到的數組。所以你需要從數組中提取值來驗證它們的有效性。這樣做的原因是,你可以有多個收件人,因此,例如對象可以返回: [「阿龍測試」,「雷諾阿」]


據我瞭解,你不需要使用方括號,並且不需要在字符串中包含引號。

這應該工作:

from: "Aaron Test <[email protected]>", 
reply_to: "Aaron Test <[email protected]>" 
+0

我同意,應該工作。我嘗試了3種不同的方式,測試結果相同。我猜測我需要測試一種不同的方法。也許渲染電子郵件視圖並解析標題。 –

+0

我已經更新了我的答案。 – frandroid

+0

不,這不起作用。我發佈了一個顯示如何測試標題值的答案。 –

相關問題