AnA*_*ice 15 rspec rspec2 email-spec
我正在使用带有email-spec gem的rspec.我正在尝试:
last_delivery = ActionMailer::Base.deliveries.last
last_delivery.body.should include "This is the text of the email"
Run Code Online (Sandbox Code Playgroud)
但这不起作用,有没有办法说出身体,文字版本?内容类型:文字/文字?
谢谢
小智 32
Body实际上是一个Mail :: Body实例.在其上调用raw_source应该可以解决问题:
last_delivery = ActionMailer::Base.deliveries.last
last_delivery.body.raw_source.should include "This is the text of the email"
Run Code Online (Sandbox Code Playgroud)
jef*_*ias 18
在尝试了上述所有选项并且无法使其正常工作之后(Rails 3.2.13),我在ruby指南中找到了一些信息(第6节是关于使用TestUnit进行测试)并使其完全按照需要工作:
last_delivery = ActionMailer::Base.deliveries.last
last_delivery.encoded.should include("This is the text of the email")
Run Code Online (Sandbox Code Playgroud)
希望有所帮助!
如果您有一个 html 模板(example.html.erb),您可以使用:
last_delivery.html_part.body.to_s
Run Code Online (Sandbox Code Playgroud)
或者,如果您有一个纯文本 ( example.text.erb ) 模板:
last_delivery.text_part.body.to_s
Run Code Online (Sandbox Code Playgroud)
来源:在 Rails 中,为什么我的邮件正文仅在我的测试中为空?
你可以随便打电话#to_s
.
last_delivery.to_s.should include "This is the text of the email"
Run Code Online (Sandbox Code Playgroud)
对于多部分电子邮件:
last_delivery.first.to_s.should include "This is the text of the email"
Run Code Online (Sandbox Code Playgroud)
测试并发现正在开发Rails 4
获取正文的通用方法:
(mail.html_part || mail.text_part || mail).body.decoded
Run Code Online (Sandbox Code Playgroud)
如果电子邮件是多部分 ( mail.multipart?
) ,则定义其mail.html_part
或,否则它们为零并返回电子邮件的内容。mail.text_part
mail.body.decoded
您可以使用也body.to_s
代替body.decoded
。
归档时间: |
|
查看次数: |
9009 次 |
最近记录: |