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

后端 未结 5 910
灰色年华
灰色年华 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:38

    If your email has attachments the text and html parts will end be placed in a multipart/alternative part. This is noted on under Sending Emails with Attachments in the Rails 3 Guide.

    To handle this, I first simplified the get_message_part method above to:

    def get_message_part(mail, content_type)
      mail.body.parts.find { |p| p.content_type.match content_type }
    end
    

    Then in my test:

    multipart = get_message_part(email, /multipart/)
    
    html = get_message_part(multipart, /html/)
    html_body = html.body.raw_source
    
    assert_match 'some string', html_body
    

提交回复
热议问题