Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rspec-email - How to get the body text?

I'm using rspec with the email-spec gem. I'm trying to do:

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

But that doesn't work, is there a way to say body, text version? Content-Type: text/text?

Thanks

like image 882
AnApprentice Avatar asked Mar 21 '11 22:03

AnApprentice


3 Answers

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"
like image 57
Alberto F. Capel Avatar answered Nov 15 '22 01:11

Alberto F. Capel


After trying all the above options and failing to get it working (Rails 3.2.13), I found some info in the ruby guide (section 6 is on testing with TestUnit) and got this to work exactly as needed:

last_delivery = ActionMailer::Base.deliveries.last
last_delivery.encoded.should include("This is the text of the email")

Hope that helps!

like image 21
jeffreymatthias Avatar answered Nov 15 '22 02:11

jeffreymatthias


If you have an html template (example.html.erb) you could use:

last_delivery.html_part.body.to_s

Or, if you have a plain-text (example.text.erb) template:

last_delivery.text_part.body.to_s

Source: In Rails why is my mail body empty only in my test?

like image 9
Giovanni Benussi Avatar answered Nov 15 '22 02:11

Giovanni Benussi