Testing ActionMailer multipart emails(text and html version) with RSpec

后端 未结 5 999
灰色年华
灰色年华 2021-01-30 22:06

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_b

5条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-30 22:26

    This can be tested with regular expressions.

    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 
    

提交回复
热议问题