Skip to content

Instantly share code, notes, and snippets.

@mcmire
Created November 2, 2016 01:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcmire/8b74ffcea161bf0b1c7024ab3134eeab to your computer and use it in GitHub Desktop.
Save mcmire/8b74ffcea161bf0b1c7024ab3134eeab to your computer and use it in GitHub Desktop.
Matcher for testing an email's body using Markdown
# You'll need to install the `reverse_markdown` gem in order for this to work.
module Matchers
def have_body_including(expected_inner_body)
HaveBodyIncludingMatcher.new(expected_inner_body)
end
class HaveBodyIncludingMatcher
def initialize(expected_inner_body)
@expected_inner_body = expected_inner_body.rstrip.gsub(\s+/, "")
end
def matches?(mail)
@mail = mail
@actual_body_as_markdown = convert_body_to_markdown
actual_body_as_markdown.include?(expected_inner_body)
end
def failure_message
"Expected the body of the email:\n\n" +
header("START") + "\n" +
actual_body_as_markdown + "\n" +
header("END") + "\n\n" +
"to contain the following text:\n\n" +
header("START") + "\n" +
expected_inner_body + "\n" +
header("END") + "\n\n" +
"but it could not be found."
end
private
attr_reader :mail, :expected_inner_body, :actual_body_as_markdown
def convert_body_to_markdown
body = Nokogiri.parse(mail.default_part_body.to_s).at_css("body").to_html
markdown = ReverseMarkdown.convert(body, unknown_tags: :bypass)
markdown.split("\n").map(&:strip).join("\n")
end
def header(text)
"~~~~#{text}" + ("~" * 50)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment