rspec-email - How to get the body text?
Asked Answered
G

8

20

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

Gaol answered 21/3, 2011 at 22:25 Comment(0)
W
40

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"
Waylan answered 22/3, 2011 at 11:31 Comment(4)
Just a quick side note: if you're using multi-part e-mails, you first need to select the correct part like so: last_delivery.parts.first.body.raw_source.should include "This is the text of the email"Gossoon
You juste made my day @PascalLindelauf! ThanksKnead
Read the comment save my day :))))))))Zaremski
This comment also saved me an hour of work @PascalLindelauf - thank you!Scolopendrid
A
20

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!

Androgynous answered 11/6, 2013 at 23:38 Comment(2)
This can actually fail when the encoded mail contains line breaks marked by "=" like "This is the=\ntext of the email"Moreta
yes, but you can delete those new lines with: last_delivery.encoded.should.delete("=\n")Horsy
A
10

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?

Acrilan answered 14/12, 2017 at 2:13 Comment(0)
U
5

you can just call #to_s on it.

last_delivery.to_s.should include "This is the text of the email"

For multipart emails:

last_delivery.first.to_s.should include "This is the text of the email"

Tested & found to be working on Rails 4

Uribe answered 31/10, 2013 at 10:55 Comment(1)
multi-part e-mails, you first need to select the correct part. Updated the answer. if this doesn't work. Let me knowUribe
I
5

Generic way to get body text:

(mail.html_part || mail.text_part || mail).body.decoded

If an email is multipart (mail.multipart?) then its mail.html_part or mail.text_part are defined, otherwise they are nil and mail.body.decoded returns email's content.

You can use also body.to_s instead of body.decoded.

Indecorous answered 19/3, 2019 at 19:29 Comment(0)
S
0

In Rails 4 the default last_delivery.body.should include "This is the text of the email" works fine for me.

Stephaniastephanie answered 27/8, 2014 at 0:14 Comment(0)
M
0

Actually there is no need to send a mail in order to test the content. You could just test:

expect(YourMailer.mail_to_test.body).to include "your string"
Muley answered 15/8, 2019 at 14:57 Comment(0)
K
-2
expect(last_delivery).to have_body_text(/This is the text of the email/)

source

Koser answered 2/4, 2014 at 9:43 Comment(1)
has_body_text is not a method for emailStephaniastephanie

© 2022 - 2024 — McMap. All rights reserved.