Class: Funktional::EmailAssertion

Inherits:
Assertion
  • Object
show all
Defined in:
lib/funktional/email_assertion.rb

Instance Method Summary collapse

Constructor Details

#initialize(expectations) ⇒ EmailAssertion

Returns a new instance of EmailAssertion.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/funktional/email_assertion.rb', line 4

def initialize(expectations)
  if ActionMailer::Base.deliveries.size < 1
    flunk 'No emails have been sent'
  end
  
  email = Email.find_closest(ActionMailer::Base.deliveries, expectations)
  
  expectations.each_key do |key|
    case key
      when :from
        then check_from(expectations[:from], email.from)
      when :to
        then check_to(expectations[:to], email.to)
      when :subject
        then assert_equal expectations[:subject], email.subject
      when :containing
        then check_containing(expectations[:containing], email.body)
      else
        flunk "Assertion key: [#{key}] not recognised"
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Funktional::Assertion

Instance Method Details

#check_containing(should_contain, body) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/funktional/email_assertion.rb', line 41

def check_containing(should_contain, body)
  if should_contain.is_a? Array
    should_contain.each do |should_i|
      assert_match /#{Regexp.escape(should_i)}/, body
    end
  else
    assert_match /#{Regexp.escape(should_contain)}/, body
  end
end

#check_from(expected_from, email_from) ⇒ Object



27
28
29
30
31
32
# File 'lib/funktional/email_assertion.rb', line 27

def check_from(expected_from, email_from)
  if email_from.nil?
    flunk 'email is missing a [from]'
  end
  assert_equal expected_from, email_from
end

#check_to(expected_to, email_to) ⇒ Object



34
35
36
37
38
39
# File 'lib/funktional/email_assertion.rb', line 34

def check_to(expected_to, email_to)
  if email_to.nil?
    flunk 'email is missing a [to]'
  end
  assert_equal expected_to, email_to
end