File: mailbox_spec.feature

package info (click to toggle)
ruby-rspec-rails 7.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,796 kB
  • sloc: ruby: 11,068; sh: 198; makefile: 6
file content (85 lines) | stat: -rw-r--r-- 2,932 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
Feature: Action mailbox specs

  Mailbox specs provide alternative assertions to those available in `ActiveMailbox::TestHelper` and help assert behaviour of how the email
  are routed, delivered, bounced or failed.

  Mailbox specs are marked by `type: :mailbox` or if you have set
  `config.infer_spec_type_from_file_location!` by placing them in `spec/mailboxes`.

  With mailbox specs you can:
  * `process(mail_or_attributes)` - send mail directly to the mailbox under test for `process`ing.
  * `receive_inbound_email(mail_or_attributes)` - matcher for asserting whether incoming email would route to the mailbox under test.
  * `have_been_delivered` - matcher for asserting whether an incoming email object was delivered.
  * `have_bounced` - matcher for asserting whether an incoming email object has bounced.
  * `have_failed` - matcher for asserting whether an incoming email object has failed.

  Background:
    Given action mailbox is available

  Scenario: Simple testing mail properly routed
    Given a file named "app/mailboxes/application_mailbox.rb" with:
      """ruby
      class ApplicationMailbox < ActionMailbox::Base
        routing (/^replies@/i) => :inbox
      end
      """
    And a file named "app/maiboxes/inbox_mailbox.rb" with:
      """ruby
      class InboxMailbox < ApplicationMailbox
        def process
          case mail.subject
          when (/^\[\d*\]/i)
            # ok
          when (/^\[\w*\]/i)
            bounced!
          else
            raise "Invalid email subject"
          end
        end
      end
      """
    And a file named "spec/mailboxes/inbox_mailbox_spec.rb" with:
      """ruby
      require 'rails_helper'

      RSpec.describe InboxMailbox, type: :mailbox do
        it "route email to properly mailbox" do
          expect(InboxMailbox)
            .to receive_inbound_email(to: "replies@example.com")
        end

        it "marks email as delivered when number tag in subject is valid" do
          mail = Mail.new(
            from: "replies@example.com",
            subject: "[141982763] support ticket"
          )
          mail_processed = process(mail)

          expect(mail_processed).to have_been_delivered
        end

        it "marks email as bounced when number tag in subject is invalid" do
          mail = Mail.new(
            from: "replies@example.com",
            subject: "[111X] support ticket"
          )
          mail_processed = process(mail)

          expect(mail_processed).to have_bounced
        end

        it "marks email as failed when subject is invalid" do
          mail = Mail.new(
            from: "replies@example.com",
            subject: "INVALID"
          )

          expect {
            expect(process(mail)).to have_failed
          }.to raise_error(/Invalid email subject/)
        end
      end
      """

    When I run `rspec spec/mailboxes/inbox_mailbox_spec.rb`
    Then the example should pass