/vendor/plugins/email-spec/lib/generators/email_spec/steps/templates/email_steps.rb

https://github.com/ganders/teambox · Ruby · 194 lines · 102 code · 39 blank · 53 comment · 10 complexity · 1b17bfd774cd178f2666897e09ffeaa1 MD5 · raw file

  1. # Commonly used email steps
  2. #
  3. # To add your own steps make a custom_email_steps.rb
  4. # The provided methods are:
  5. #
  6. # last_email_address
  7. # reset_mailer
  8. # open_last_email
  9. # visit_in_email
  10. # unread_emails_for
  11. # mailbox_for
  12. # current_email
  13. # open_email
  14. # read_emails_for
  15. # find_email
  16. #
  17. # General form for email scenarios are:
  18. # - clear the email queue (done automatically by email_spec)
  19. # - execute steps that sends an email
  20. # - check the user received an/no/[0-9] emails
  21. # - open the email
  22. # - inspect the email contents
  23. # - interact with the email (e.g. click links)
  24. #
  25. # The Cucumber steps below are setup in this order.
  26. module EmailHelpers
  27. def current_email_address
  28. # Replace with your a way to find your current email. e.g @current_user.email
  29. # last_email_address will return the last email address used by email spec to find an email.
  30. # Note that last_email_address will be reset after each Scenario.
  31. last_email_address || "example@example.com"
  32. end
  33. end
  34. World(EmailHelpers)
  35. #
  36. # Reset the e-mail queue within a scenario.
  37. # This is done automatically before each scenario.
  38. #
  39. Given /^(?:a clear email queue|no emails have been sent)$/ do
  40. reset_mailer
  41. end
  42. #
  43. # Check how many emails have been sent/received
  44. #
  45. Then /^(?:I|they|"([^"]*?)") should receive (an|no|\d+) emails?$/ do |address, amount|
  46. unread_emails_for(address).size.should == parse_email_count(amount)
  47. end
  48. Then /^(?:I|they|"([^"]*?)") should have (an|no|\d+) emails?$/ do |address, amount|
  49. mailbox_for(address).size.should == parse_email_count(amount)
  50. end
  51. Then /^(?:I|they|"([^"]*?)") should receive (an|no|\d+) emails? with subject "([^"]*?)"$/ do |address, amount, subject|
  52. unread_emails_for(address).select { |m| m.subject =~ Regexp.new(subject) }.size.should == parse_email_count(amount)
  53. end
  54. Then /^(?:I|they|"([^"]*?)") should receive an email with the following body:$/ do |address, expected_body|
  55. open_email(address, :with_text => expected_body)
  56. end
  57. #
  58. # Accessing emails
  59. #
  60. # Opens the most recently received email
  61. When /^(?:I|they|"([^"]*?)") opens? the email$/ do |address|
  62. open_email(address)
  63. end
  64. When /^(?:I|they|"([^"]*?)") opens? the email with subject "([^"]*?)"$/ do |address, subject|
  65. open_email(address, :with_subject => subject)
  66. end
  67. When /^(?:I|they|"([^"]*?)") opens? the email with text "([^"]*?)"$/ do |address, text|
  68. open_email(address, :with_text => text)
  69. end
  70. #
  71. # Inspect the Email Contents
  72. #
  73. Then /^(?:I|they) should see "([^"]*?)" in the email subject$/ do |text|
  74. current_email.should have_subject(text)
  75. end
  76. Then /^(?:I|they) should see \/([^"]*?)\/ in the email subject$/ do |text|
  77. current_email.should have_subject(Regexp.new(text))
  78. end
  79. Then /^(?:I|they) should see "([^"]*?)" in the email body$/ do |text|
  80. current_email.default_part_body.to_s.should include(text)
  81. end
  82. Then /^(?:I|they) should see \/([^"]*?)\/ in the email body$/ do |text|
  83. current_email.default_part_body.to_s.should =~ Regexp.new(text)
  84. end
  85. Then /^(?:I|they) should see the email delivered from "([^"]*?)"$/ do |text|
  86. current_email.should be_delivered_from(text)
  87. end
  88. Then /^(?:I|they) should see "([^\"]*)" in the email "([^"]*?)" header$/ do |text, name|
  89. current_email.should have_header(name, text)
  90. end
  91. Then /^(?:I|they) should see \/([^\"]*)\/ in the email "([^"]*?)" header$/ do |text, name|
  92. current_email.should have_header(name, Regexp.new(text))
  93. end
  94. Then /^I should see it is a multi\-part email$/ do
  95. current_email.should be_multipart
  96. end
  97. Then /^(?:I|they) should see "([^"]*?)" in the email html part body$/ do |text|
  98. current_email.html_part.body.to_s.should include(text)
  99. end
  100. Then /^(?:I|they) should see "([^"]*?)" in the email text part body$/ do |text|
  101. current_email.text_part.body.to_s.should include(text)
  102. end
  103. #
  104. # Inspect the Email Attachments
  105. #
  106. Then /^(?:I|they) should see (an|no|\d+) attachments? with the email$/ do |amount|
  107. current_email_attachments.size.should == parse_email_count(amount)
  108. end
  109. Then /^there should be (an|no|\d+) attachments? named "([^"]*?)"$/ do |amount, filename|
  110. current_email_attachments.select { |a| a.filename == filename }.size.should == parse_email_count(amount)
  111. end
  112. Then /^attachment (\d+) should be named "([^"]*?)"$/ do |index, filename|
  113. current_email_attachments[(index.to_i - 1)].filename.should == filename
  114. end
  115. Then /^there should be (an|no|\d+) attachments? of type "([^"]*?)"$/ do |amount, content_type|
  116. current_email_attachments.select { |a| a.content_type.include?(content_type) }.size.should == parse_email_count(amount)
  117. end
  118. Then /^attachment (\d+) should be of type "([^"]*?)"$/ do |index, content_type|
  119. current_email_attachments[(index.to_i - 1)].content_type.should include(content_type)
  120. end
  121. Then /^all attachments should not be blank$/ do
  122. current_email_attachments.each do |attachment|
  123. attachment.read.size.should_not == 0
  124. end
  125. end
  126. Then /^show me a list of email attachments$/ do
  127. EmailSpec::EmailViewer::save_and_open_email_attachments_list(current_email)
  128. end
  129. #
  130. # Interact with Email Contents
  131. #
  132. When /^(?:I|they) follow "([^"]*?)" in the email$/ do |link|
  133. visit_in_email(link)
  134. end
  135. When /^(?:I|they) click the first link in the email$/ do
  136. click_first_link_in_email
  137. end
  138. #
  139. # Debugging
  140. # These only work with Rails and OSx ATM since EmailViewer uses RAILS_ROOT and OSx's 'open' command.
  141. # Patches accepted. ;)
  142. #
  143. Then /^save and open current email$/ do
  144. EmailSpec::EmailViewer::save_and_open_email(current_email)
  145. end
  146. Then /^save and open all text emails$/ do
  147. EmailSpec::EmailViewer::save_and_open_all_text_emails
  148. end
  149. Then /^save and open all html emails$/ do
  150. EmailSpec::EmailViewer::save_and_open_all_html_emails
  151. end
  152. Then /^save and open all raw emails$/ do
  153. EmailSpec::EmailViewer::save_and_open_all_raw_emails
  154. end