Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Email spec doesn't match body content in Rails

I'm using email_spec gem to test a simple email, but for some reason the body content appears to be empty:

  1) ContactMailer welcome email to new user renders the body
     Failure/Error: mail.should have_body_text("Hi")
       expected the body to contain "Hi" but was ""
     # ./spec/mailers/contact_mailer_spec.rb:17:in `block (3 levels) in <top (required)>'

Every other example passes. The template file is called welcome_email.text.erb. Not sure why body is not matched, but the email does have a body when it gets sent.

Edit: the Rspec code is:

let(:mail) { ContactMailer.welcome_email(email) }


it "renders the body" do
  mail.should have_body_text("Hi")
end
like image 316
picardo Avatar asked Oct 21 '25 20:10

picardo


1 Answers

The best way I've found to do this is:

it "contains a greeting" do
  mail.html_part.body.should match /Hi/
end

You can also use text_part in place of html_part if you want to check the plain text part of a multipart message.

Also note that others may recommend using #encoded, but I had trouble using that with long URLs, as they may get line-wrapped during the encoding process.

like image 111
Matt Green Avatar answered Oct 23 '25 09:10

Matt Green



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!