|
| 1 | +@rails_post_6 |
| 2 | +Feature: action mailbox spec |
| 3 | + |
| 4 | + Mailbox specs provide alternative assertions to those available in `ActiveMailbox::TestHelper` and help assert behaviour of how the email |
| 5 | + are routed, delivered, bounced or failed. |
| 6 | + |
| 7 | + Mailbox specs are marked by `type: :mailbox` or if you have set |
| 8 | + `config.infer_spec_type_from_file_location!` by placing them in `spec/mailboxes`. |
| 9 | + |
| 10 | + With mailbox specs you can: |
| 11 | + * `process(mail_or_attributes)` - send mail directly to the mailbox under test for `process`ing. |
| 12 | + * `receive_inbound_email(mail_or_attributes)` - matcher for asserting whether incoming email would route to the mailbox under test. |
| 13 | + * `have_been_delivered` - matcher for asserting whether an incoming email object was delivered. |
| 14 | + * `have_bounced` - matcher for asserting whether an incoming email object has bounced. |
| 15 | + * `have_failed` - matcher for asserting whether an incoming email object has failed. |
| 16 | + |
| 17 | + Background: |
| 18 | + Given action mailbox is available |
| 19 | + |
| 20 | + Scenario: Simple testing mail properly routed |
| 21 | + Given a file named "app/mailboxes/application_mailbox.rb" with: |
| 22 | + """ruby |
| 23 | + class ApplicationMailbox < ActionMailbox::Base |
| 24 | + routing (/^replies@/i) => :inbox |
| 25 | + end |
| 26 | + """ |
| 27 | + And a file named "app/maiboxes/inbox_mailbox.rb" with: |
| 28 | + """ruby |
| 29 | + class InboxMailbox < ApplicationMailbox |
| 30 | + def process |
| 31 | + case mail.subject |
| 32 | + when (/^\[\d*\]/i) |
| 33 | + # ok |
| 34 | + when (/^\[\w*\]/i) |
| 35 | + bounced! |
| 36 | + else |
| 37 | + raise "Invalid email subject" |
| 38 | + end |
| 39 | + end |
| 40 | + end |
| 41 | + """ |
| 42 | + And a file named "spec/mailboxes/inbox_mailbox_spec.rb" with: |
| 43 | + """ruby |
| 44 | + require 'rails_helper' |
| 45 | +
|
| 46 | + RSpec.describe InboxMailbox, type: :mailbox do |
| 47 | + it "route email to properly mailbox" do |
| 48 | + expect(InboxMailbox) |
| 49 | + .to receive_inbound_email(to: "[email protected]") |
| 50 | + end |
| 51 | +
|
| 52 | + it "marks email as bounced when number tag in subject is valid" do |
| 53 | + mail = Mail.new( |
| 54 | + |
| 55 | + subject: "[141982763] support ticket" |
| 56 | + ) |
| 57 | + mail_processed = process(mail) |
| 58 | +
|
| 59 | + expect(mail_processed).to have_been_delivered |
| 60 | + end |
| 61 | +
|
| 62 | + it "marks email as bounced when number tag in subject is invalid" do |
| 63 | + mail = Mail.new( |
| 64 | + |
| 65 | + subject: "[111X] support ticket" |
| 66 | + ) |
| 67 | + mail_processed = process(mail) |
| 68 | +
|
| 69 | + expect(mail_processed).to have_bounced |
| 70 | + end |
| 71 | +
|
| 72 | + it "marks email as failed when subject is invalid" do |
| 73 | + mail = Mail.new( |
| 74 | + |
| 75 | + subject: "INVALID" |
| 76 | + ) |
| 77 | +
|
| 78 | + expect { |
| 79 | + expect(process(mail)).to have_failed |
| 80 | + }.to raise_error(/Invalid email subject/) |
| 81 | + end |
| 82 | + end |
| 83 | + """ |
| 84 | + |
| 85 | + When I run `rspec spec/mailboxes/inbox_mailbox_spec.rb` |
| 86 | + Then the example should pass |
0 commit comments