Ruby on rails 3 Rails-如何测试ActionMailer发送的特定附件?

Ruby on rails 3 Rails-如何测试ActionMailer发送的特定附件?,ruby-on-rails-3,unit-testing,ruby-on-rails-3.2,actionmailer,Ruby On Rails 3,Unit Testing,Ruby On Rails 3.2,Actionmailer,在我的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@

在我的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
}
@expected.to=BuyadsproMailer.group\u to(campaign.agency.users)
@expected.subject=“您为{offer\u log.campaign.name}提交了价值{offer\u log.total}的报价”
@expected.from=“BuyAds Pro”
@expected.body=读取夹具(“交付给机构”)
@expected.content\u type=“multipart/mixed;”\r\n boundary=\“something”
@应为.attachments[“#{offer_log.aws_key}.pdf”]={
:mime_type=>“application/pdf”,
:content=>fake_pdf.body
}
并将我的邮件存根以获取假的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...>
应为,但为
我与生成的电子邮件的字符集或部分边界不匹配


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

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


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

expect(mail.attachments.first.body.encoded.gsub(/\r/, '')).to(
  eq(
    <<~CSV
      "Foo","Bar"
      "1","2"
    CSV
  )
) 
expect(mail.attachments.first.body.encoded.gsub(/\r/,“”))到(
情商(

有没有办法比较md5散列?答案不错。不过,检查结果是否是一种
Mail::Part
似乎没有必要,如果类名在Rails的未来版本中不同,可能会使测试变得不必要的脆弱。如果对象的类型不正确,那么它响应
内容类型的可能性会降低
filename
方法非常低。您还可以测试附件的内容:
expect(attachment.body)。to eq fake_pdf.body
expect(mail.attachments.first.body.encoded.gsub(/\r/, '')).to(
  eq(
    <<~CSV
      "Foo","Bar"
      "1","2"
    CSV
  )
)