本文介绍了使用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

我同时拥有文本和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部分中查找内容(此后应使用#应该匹配):

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

在纯文本部分中查找内容(此后应使用#应该匹配):

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版本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 20:34