Home > OS >  Rspec doesnt show the content in mail body
Rspec doesnt show the content in mail body

Time:12-16

while testing my mail contents, in actual mail it displays the mail contents, but while testing my mail in rspec it does'nt shows the content, as it shows only the email image attachment, previously without sending the attachment in mail, it shows the email body in rspec, but now it doest show the contents in rspec after attaching image attachment, so how to fetch the content

my rspec result

#<Mail::Body:0x00555c3bbcef70

 @boundary="--==_mimepart_61bb2dce1d6b1_f92aae1ba4534536817",

         @charset="US-ASCII",
         @encoding="7bit",
         @epilogue=nil,
         @part_sort_order=["text/plain", "text/enriched", "text/html"],

         @parts=
          [#<Mail::Part:46927317154560, Multipart: false, Headers: <Content-Type: text/html>>,

           #<Mail::Part:46927293531360, Multipart: false, Headers: <Content-Type: image/jpeg; filename="image.jpg">, <Content-Transfer-Encoding: binary>, <Content-Disposition: attachment; filename="image.jpg">, <Content-ID: <[email protected]>>>,

           #<Mail::Part:46927312188120, Multipart: false, Headers: <Content-Type: image/png; filename="user2_app.png">, <Content-Transfer-Encoding: binary>, <Content-Disposition: attachment; filename="user2_app.png">, <Content-ID: <[email protected]>>>,

           #<Mail::Part:469273513434, Multipart: false, Headers: <Content-Type: image/png; filename="user2_app.png">, <Content-Transfer-Encoding: binary>, <Content-Disposition: attachment; filename="user2.png">, <Content-ID: <[email protected]>>>,
           ,
         @preamble=nil,
 @raw_source="">

my rspec method

 mail = Mailer.send_mail_to_user(user_name,address)

expect(mail.body).to include("Welcome user")

but in my actual mail while sending it includes the text welcome user

but in rspec it doesnt shows that?

how to test that , pls help me out

CodePudding user response:

expect(mail.body).to include("Welcome user") will only actually work if you're sending a simple email with only text. When you're sending a multipart email you need to match the specific part of the email:

let(:html_body) do
  mail.body.parts.find { |p| p.content_type.match 'text/html' }.body.raw_source
end 

it "contains hello world" do
  expect(html_body).to include("Welcome user")
end
  • Related