Created
November 2, 2016 01:53
-
-
Save mcmire/8b74ffcea161bf0b1c7024ab3134eeab to your computer and use it in GitHub Desktop.
Matcher for testing an email's body using Markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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