Testing Actionmailer Multipart Emails(Text and HTML Version) with Rspec

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

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

Testing with RSpec and Action Mailer: Clearing enqueued emails?

Yes you can configure rspec to clear the mailers before each test like:

RSpec.configure do |config|
config.before(:each) do
ActionMailer::Base.deliveries.clear
end
end

In case of enqueue mails, Active Job Test Helper is included in ActiveMailer::TestHelper so you should be able to use the clear_enqueued_jobs method too.

Source: https://github.com/rails/rails/blob/94b5cd3a20edadd6f6b8cf0bdf1a4d4919df86cb/actionmailer/lib/action_mailer/test_helper.rb#L9

rspec-email - How to get the body text?

Body is actually a Mail::Body instance. Calling raw_source on it should do the trick:

last_delivery = ActionMailer::Base.deliveries.last
last_delivery.body.raw_source.should include "This is the text of the email"

Rails: How to use a partial in multipart/alternative emails (HTML and plain text)

I found the answer that works for me. PeterD posted the following on rails mailer with different layouts:

...the layouts follow a different naming scheme to the email templates. Just rename them as follows:

layout.text.html.erb    => layout.html.erb
layout.text.plain.erb => layout.text.erb


Related Topics



Leave a reply



Submit