32

在我的 ActionMailer::TestCase 测试中,我期待:

@expected.to      = BuyadsproMailer.group_to(campaign.agency.users)
@expected.subject = "You submitted #{offer_log.total} worth of offers for #{offer_log.campaign.name} "
@expected.from    = "BuyAds Pro <feedback@buyads.com>"
@expected.body    = read_fixture('deliver_to_agency')

@expected.content_type = "multipart/mixed;\r\n boundary=\"something\""
@expected.attachments["#{offer_log.aws_key}.pdf"] = {
  :mime_type => 'application/pdf',
  :content => fake_pdf.body
}

并存根我的邮件以获取 fake_pdf 而不是通常从 S3 获取的真实 PDF,以便我确定 PDF 的正文匹配。

但是,我收到这个很长的错误,告诉我应该收到一封电子邮件,但收到的电子邮件略有不同:

<...Mime-Version: 1.0\r\nContent-Type: multipart/mixed\r\nContent-Transfer-Encoding: 7bit...> expected but was
<...Mime-Version: 1.0\r\nContent-Type: multipart/mixed;\r\n boundary=\"--==_mimepart_50f06fa9c06e1_118dd3fd552035ae03352b\";\r\n charset=UTF-8\r\nContent-Transfer-Encoding: 7bit...>

我不匹配生成的电子邮件的字符集或部分边界。

如何定义或存根预期电子邮件的这一方面?

4

2 回答 2

54

这是我从特定附件的 rspec 测试中复制的示例,希望对您有所帮助(可以通过调用您的 mailer 方法或在调用 .deliver 后查看交付数组来创建邮件):

  mail.attachments.should have(1).attachment
  attachment = mail.attachments[0]
  attachment.should be_a_kind_of(Mail::Part)
  attachment.content_type.should be_start_with('application/ics;')
  attachment.filename.should == 'event.ics'
于 2013-02-13T23:38:12.730 回答
3

我有类似的东西,我想检查附加的 csv 内容。我需要这样的东西,因为它看起来像是\r插入换行符:

expect(mail.attachments.first.body.encoded.gsub(/\r/, '')).to(
  eq(
    <<~CSV
      "Foo","Bar"
      "1","2"
    CSV
  )
) 
于 2020-06-17T18:42:41.097 回答