Class: MailChecker::ExpectationMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/mail_checker/expectation_matcher.rb

Instance Method Summary collapse

Instance Method Details

#failure_messageObject



31
32
33
34
# File 'lib/mail_checker/expectation_matcher.rb', line 31

def failure_message
  "A mail with #{@attributes} was expected to be delivered but was not.\n" +
  "List of received mails: #{MailChecker::Mail.all.map(&:attributes)}"
end

#matches?(expectation) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/mail_checker/expectation_matcher.rb', line 5

def matches?(expectation)
  attributes = {}
  attributes['subject'] = expectation.subject if expectation.subject
  attributes['sender'] = expectation.from if expectation.from
  attributes['recipients'] = expectation.to if expectation.to
  attributes.reverse_merge!(expectation.attributes) if expectation.attributes

  @attributes = attributes

  begin
    Timeout::timeout(5) do
      loop do
        break if MailChecker::Mail.any? do |mail|
          attributes.all? { |a, v| attribute_matches?([*mail.attributes[a]], to_regexp([*v])) }
        end

        sleep 0.5
      end
    end
  rescue Timeout::Error
    false
  else
    true
  end
end