Rails-如何测试ActionMailer发送了特定附件? [英] Rails - How to test that ActionMailer sent a specific attachment?

查看:86
本文介绍了Rails-如何测试ActionMailer发送了特定附件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my ActionMailer::TestCase test, I'm expecting:

@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而不是通常的真实PDF从S3获取,以便我确定PDF的主体匹配。

and stub my mailer to get fake_pdf instead of a real PDF normally fetched from S3 so that I'm sure the bodies of the PDFs match.

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

However, I get this long error telling me that one email was expected but got a slightly different email:

<...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...>

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

I'm not matching the charset or part-boundary of the generated email.

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

How do I define or stub this aspect of my expected emails?

推荐答案

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

Here's an example that I copied from my rspec test of a specific attachment, hope that it helps (mail can be creating by calling your mailer method or peeking at the deliveries array after calling .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'

这篇关于Rails-如何测试ActionMailer发送了特定附件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆