Checking the mail program link in rspec

In my rspec testing, I have this comparison:

expect(new_mail.body.encoded).to match(url_for(controller:'some_controller',action:'some_action',some_param:some_param))

but it fails because ActionMailer encodes the html body like this:

   +<a href=3D"http://localhost:3000/some_controller/some_action/wgt1p468ersmkq=
   +gbvmfv5wgmifj13u894dfjrhc0hzczk71pcw3hrk5907iqfolc6onhxvik6apuuwgm1rng7ro=
   +rt8qih43thql3spyp5ajjdugy9jvx0xc5hvpi015z" style=3D"display: inline-block=
   +; background-color: #71B344; color: #FFF; border-radius: 4px; font-weight=
   +: bold; text-decoration: none; padding: 6px 12px; white-space: nowrap">
   +        Go to Your Account
   +      </a>

how to compare the expected link and the encoded link in the body of the letter?

+4
source share
1 answer

Instead, you can use new_mail.body.to_sit to get an uncoded version, as described in the Rails Testing Guide .

Capybara, ( html), , , :

let(:email){ Capybara::Node::Simple.new(new_mail.body.to_s) }

it "has a link" do
  url = url_for(controller:'some_controller',action:'some_action',some_param:some_param)
  expect(email).to have_link('A link', href: url)
end

:

:

def get_message_part(mail, content_type)
  mail.body.parts.find { |p| p.content_type.match content_type }.body.raw_source
end

let(:email) { Capybara::Node::Simple.new(get_message_part(new_mail, /html/)) }
+4

All Articles