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
Rails Generate Has_Many Association
Rails: Serializing Objects in a Database
Contact Form in Ruby, Sinatra, and Haml
Activerecord Objects in Hashes Aren't Garbage Collected -- a Bug or a Sort of Caching Feature
Using a Ruby Script to Login to a Website via Https
Error Installing Any Ruby Version with Rvm on Osx
Ruby Koans: Why Convert List of Symbols to Strings
Rails 5.2.0 with Ruby 2.5.1 Console - 'Warning:' 'Already' Initialized Constant Fileutils::Version
Suitability of Rails, Padrino, and Sinatra for Building a Prepaid Mobile Service
Ruby Methods That Either Yield or Return Enumerator
Rails: Ensure Only One Boolean Field Is Set to True at a Time
How to Read a Barcode from an Image
Iterate Every Month with Date Objects
How to Remove/Disable Sign Up from Devise