Module: Pony::TestHelpers

Defined in:
lib/pony-test.rb

Constant Summary collapse

@@deliveries =
[]
@@current_email =
nil
@@current_email_address =
nil

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.deliver(email) ⇒ Object



18
19
20
21
# File 'lib/pony-test.rb', line 18

def self.deliver(email)
  @@deliveries << email
  @@current_email = email
end

Instance Method Details

#current_emailObject



28
29
30
# File 'lib/pony-test.rb', line 28

def current_email
  @@current_email || raise('No current email')
end

#current_email=(email) ⇒ Object



32
33
34
# File 'lib/pony-test.rb', line 32

def current_email=(email)
  @@current_email = email
end

#current_email_addressObject



36
37
38
# File 'lib/pony-test.rb', line 36

def current_email_address
  @@current_email_address
end

#current_email_address=(address) ⇒ Object



40
41
42
# File 'lib/pony-test.rb', line 40

def current_email_address=(address)
  @@current_email_address = address
end

#deliveriesObject Also known as: all_email



23
24
25
# File 'lib/pony-test.rb', line 23

def deliveries
  @@deliveries
end


102
103
104
105
106
# File 'lib/pony-test.rb', line 102

def email_links(email = current_email)
  links = URI.extract(email.body, ['http', 'https'])
  raise "No links found in #{email.body}" if links.nil? || links.empty?
  links
end


108
109
110
111
112
113
# File 'lib/pony-test.rb', line 108

def email_links_matching(pattern, email = current_email)
  pattern = /#{Regexp.escape(pattern)}/ unless pattern.is_a?(Regexp)
  links = email_links(email).select { |link| link =~ pattern }
  raise "No link found matching #{pattern.inspect} in #{email.body}" if links.nil? || links.empty?
  links
end

#find_email_for(opts = {}) ⇒ Object Also known as: find_email



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/pony-test.rb', line 83

def find_email_for(opts = {})
  email = inbox_for(opts)

  if opts[:with_subject]
    email = email.select { |m| m.subject =~ Regexp.new(opts[:with_subject]) }
  end

  if opts[:with_body]
    email = email.select { |m| m.body =~ Regexp.new(opts[:with_body]) }
  end

  if email.empty?
    raise "Could not find email. \n Found the following email:\n\n #{deliveries.to_s}"
  end

  email
end

#inbox_for(opts = {}) ⇒ Object Also known as: inbox



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/pony-test.rb', line 55

def inbox_for(opts = {})
  address = opts[:address]
  if address.nil? || address.empty?
    address = self.current_email_address
  end

  unless address.nil? || address.empty?
    self.current_email_address = address
  end

  #current_email_address should either be nil or have a specific address
  if current_email_address.nil?
    deliveries
  else
    deliveries.select do |email| 
      (email.to && email.to.include?(address)) || 
        (email.bcc && email.bcc.include?(address)) || 
        (email.cc && email.cc.include?(address))
    end
  end
end

#last_email_sentObject



50
51
52
53
# File 'lib/pony-test.rb', line 50

def last_email_sent
  self.current_email = deliveries.last
  self.current_email
end

#open_email_for(opts = {}) ⇒ Object Also known as: open_email



78
79
80
# File 'lib/pony-test.rb', line 78

def open_email_for(opts = {})
  self.current_email = find_email(opts).first
end

#reset_mailerObject



44
45
46
47
48
# File 'lib/pony-test.rb', line 44

def reset_mailer
  self.deliveries.clear
  self.current_email_address = nil
  self.current_email = nil
end