Module: EmailSpec::Helpers

Includes:
Deliveries
Defined in:
lib/email_spec/helpers.rb

Instance Method Summary collapse

Methods included from Deliveries

#deliveries

Methods included from BackgroundProcesses::DelayedJob

#all_emails, #last_email_sent, #reset_mailer

Methods included from MailerDeliveries

#all_emails, #last_email_sent, #reset_mailer

Instance Method Details



13
14
15
16
17
# File 'lib/email_spec/helpers.rb', line 13

def click_email_link_matching(regex, email = current_email)
  url = links_in_email(email).detect { |link| link =~ regex }
  raise "No link found matching #{regex.inspect} in #{email.default_part_body}" unless url
  visit request_uri(url)
end


19
20
21
22
# File 'lib/email_spec/helpers.rb', line 19

def click_first_link_in_email(email = current_email)
  link = links_in_email(email).first
  visit request_uri(link)
end

#current_email(address = nil) ⇒ Object

Raises:

  • (RSpec::Expectations::ExpectationNotMetError)


38
39
40
41
42
43
# File 'lib/email_spec/helpers.rb', line 38

def current_email(address=nil)
  address = convert_address(address)
  email = address ? email_spec_hash[:current_emails][address] : email_spec_hash[:current_email]
  raise   RSpec::Expectations::ExpectationNotMetError, "Expected an open email but none was found. Did you forget to call open_email?" unless email
  email
end

#current_email_attachments(address = nil) ⇒ Object



45
46
47
# File 'lib/email_spec/helpers.rb', line 45

def current_email_attachments(address=nil)
  current_email(address).attachments || Array.new
end

#find_email(address, opts = {}) ⇒ Object

Should be able to accept String or Regexp options.



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/email_spec/helpers.rb', line 58

def find_email(address, opts={})
  address = convert_address(address)
  if opts[:with_subject]
    expected_subject = (opts[:with_subject].is_a?(String) ? Regexp.escape(opts[:with_subject]) : opts[:with_subject])
    mailbox_for(address).find { |m| m.subject =~ Regexp.new(expected_subject) }
  elsif opts[:with_text]
    expected_text = (opts[:with_text].is_a?(String) ? Regexp.escape(opts[:with_text]) : opts[:with_text])
    mailbox_for(address).find { |m| m.default_part_body =~ Regexp.new(expected_text) }
  else
    mailbox_for(address).first
  end
end


71
72
73
# File 'lib/email_spec/helpers.rb', line 71

def links_in_email(email)
  URI.extract(email.default_part_body.to_s, ['http', 'https'])
end

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



24
25
26
# File 'lib/email_spec/helpers.rb', line 24

def open_email(address, opts={})
  set_current_email(find_email!(address, opts))
end

#open_last_emailObject



30
31
32
# File 'lib/email_spec/helpers.rb', line 30

def open_last_email
  set_current_email(last_email_sent)
end

#open_last_email_for(address) ⇒ Object



34
35
36
# File 'lib/email_spec/helpers.rb', line 34

def open_last_email_for(address)
  set_current_email(mailbox_for(address).last)
end

#read_emails_for(address) ⇒ Object



53
54
55
# File 'lib/email_spec/helpers.rb', line 53

def read_emails_for(address)
  email_spec_hash[:read_emails][convert_address(address)] ||= []
end

#unread_emails_for(address) ⇒ Object



49
50
51
# File 'lib/email_spec/helpers.rb', line 49

def unread_emails_for(address)
  mailbox_for(address) - read_emails_for(address)
end

#visit_in_email(link_text) ⇒ Object



9
10
11
# File 'lib/email_spec/helpers.rb', line 9

def visit_in_email(link_text)
  visit(parse_email_for_link(current_email, link_text))
end