Search code examples
ruby-on-railsrubyruby-on-rails-4rspecrspec-rails

Get full text content of html email in RSpec


How do i get the full text content of an html email as single line string in rspec?

Let's say this is the mail.html.haml:

%p 
  This product is worth: 
  = @amount
  So buy it now!

In rspec i send the email first and then do this:

expect(ActionMailer::Base.deliveries.last.html_part.body).to include("This product is worth: €30 So buy it now")

or:

expect(ActionMailer::Base.deliveries.last.html_part.body.raw_source).to include("This product is worth: €30 So buy it now")

or:

expect(ActionMailer::Base.deliveries.last.html_part.body.raw_source).to have_content("This product is worth: €30 So buy it now")

These doesnt work because the html outputs to rspec like this:

 + This product is worth:
 + €30
 + So buy it now!

So my original code is right but i cant seem to grab it in rspec. How do i combine these 3 lines to 1 single line so i can call include or have_content on it correctly?

*EDIT

Failure/Error:
       expect(ActionMailer::Base.deliveries.last.html_part.body.raw_source).to have_content(%q|This product is worth:
                                                                                               €30
                                                                                               So buy it now!|)

       expected        
       <p>
       This product is worth:
       €30
       So buy it now!
       Good luck!
       </p>

Solution

  • The answer was simple. The HAML needs to be inline like this:

    %p This product is worth: #{@amount} So buy it now!
    

    or the variable needs to be interpolated like this:

    %p 
      This product is worth: 
      #{@amount}
      So buy it now!