使用 RSpec 测试 ActionMailer 多部分电子邮件(文本和 html 版本) [英] Testing ActionMailer multipart emails(text and html version) with RSpec

查看:33
本文介绍了使用 RSpec 测试 ActionMailer 多部分电子邮件(文本和 html 版本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 RSpec 测试我的邮件程序,但我已经开始按照此处的 Rails 指南中的说明设置多部分电子邮件:http://guides.rubyonrails.org/action_mailer_basics.html#sending-multipart-emails

I'm currently testing my mailers with RSpec, but I've started setting up multipart emails as described in the Rails Guides here: http://guides.rubyonrails.org/action_mailer_basics.html#sending-multipart-emails

我有 text 和 html 格式的邮件模板,但看起来我的测试只检查 HTML 部分.有没有办法单独检查文本模板?

I have both mailer templates in text and html formats, but it looks like my tests are only checking the HTML portion. Is there a way to check the text template separately?

是否只检查 HTML 视图,因为它是默认顺序中的第一个?

Is it only checking the HTML view because it's first in the default order?

推荐答案

这可以用正则表达式进行测试.

This can be tested with regular expressions.

在 HTML 部分中查找内容(在此之后使用 #should 进行匹配):

Finding things in the HTML portion (use #should after this to match):

mail.body.parts.find {|p| p.content_type.match /html/}.body.raw_source

在纯文本部分查找内容(在此之后使用 #should 进行匹配):

Finding things in the plain text portion (use #should after this to match):

mail.body.parts.find {|p| p.content_type.match /plain/}.body.raw_source

检查它是否确实生成了多部分消息:

Checking that it is, indeed, generating a multipart message:

it "generates a multipart message (plain text and html)" do
  mail.body.parts.length.should == 2
  mail.body.parts.collect(&:content_type).should == ["text/html; charset=UTF-8", "text/plain; charset=UTF-8"]
end 

这篇关于使用 RSpec 测试 ActionMailer 多部分电子邮件(文本和 html 版本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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