Module: EmailSpec::Helpers
- Includes:
- Deliveries
- Defined in:
- lib/email_spec/helpers.rb
Instance Method Summary
collapse
Methods included from Deliveries
#deliveries
#all_emails, #last_email_sent, #reset_mailer
#all_emails, #last_email_sent, #reset_mailer
Instance Method Details
#click_email_link_matching(regex, email = current_email) ⇒ Object
18
19
20
21
22
|
# File 'lib/email_spec/helpers.rb', line 18
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
|
#click_first_link_in_email(email = current_email) ⇒ Object
24
25
26
27
|
# File 'lib/email_spec/helpers.rb', line 24
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
43
44
45
46
47
48
|
# File 'lib/email_spec/helpers.rb', line 43
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
50
51
52
|
# File 'lib/email_spec/helpers.rb', line 50
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.
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/email_spec/helpers.rb', line 63
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) }
elsif opts[:from]
mailbox_for(address).find { |m| m.from.include? opts[:from] }
else
mailbox_for(address).first
end
end
|
#links_in_email(email) ⇒ Object
78
79
80
|
# File 'lib/email_spec/helpers.rb', line 78
def links_in_email(email)
URI.(email.default_part_body.to_s, ['http', 'https'])
end
|
#open_email(address, opts = {}) ⇒ Object
Also known as:
open_email_for
29
30
31
|
# File 'lib/email_spec/helpers.rb', line 29
def open_email(address, opts={})
set_current_email(find_email!(address, opts))
end
|
#open_last_email ⇒ Object
35
36
37
|
# File 'lib/email_spec/helpers.rb', line 35
def open_last_email
set_current_email(last_email_sent)
end
|
#open_last_email_for(address) ⇒ Object
39
40
41
|
# File 'lib/email_spec/helpers.rb', line 39
def open_last_email_for(address)
set_current_email(mailbox_for(address).last)
end
|
#read_emails_for(address) ⇒ Object
58
59
60
|
# File 'lib/email_spec/helpers.rb', line 58
def read_emails_for(address)
email_spec_hash[:read_emails][convert_address(address)] ||= []
end
|
#unread_emails_for(address) ⇒ Object
54
55
56
|
# File 'lib/email_spec/helpers.rb', line 54
def unread_emails_for(address)
mailbox_for(address) - read_emails_for(address)
end
|
#visit_in_email(link_text, address = '') ⇒ Object
9
10
11
12
13
14
15
16
|
# File 'lib/email_spec/helpers.rb', line 9
def visit_in_email(link_text, address = '')
if address.nil? || address.empty?
email = current_email
else
email = find_email!(address)
end
visit(parse_email_for_link(email, link_text))
end
|