Skip to content

Commit 9ce4e65

Browse files
committed
Be able to feature test action mailbox against our example app
File are generated by the feature test itself Add missing newline at the end of file Avoid too long line for Relish doc
1 parent 2d28fc1 commit 9ce4e65

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

example_app_generator/generate_stuff.rb

+6
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ def using_source_path(path)
104104
copy_file 'spec/features/model_mocks_integration_spec.rb'
105105
end
106106

107+
begin
108+
require 'action_mailbox'
109+
run('rails action_mailbox:install')
110+
rescue LoadError
111+
end
112+
107113
begin
108114
require 'active_job'
109115
generate('job upload_backups')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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

features/step_definitions/additional_cli_steps.rb

+11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
require "action_cable"
77
rescue LoadError # rubocop:disable Lint/SuppressedException
88
end
9+
begin
10+
require "active_storage"
11+
require "action_mailbox"
12+
rescue LoadError # rubocop:disable Lint/SuppressedException
13+
end
914

1015
require "rails/version"
1116

@@ -32,3 +37,9 @@
3237
pending "Action Cable testing is not available"
3338
end
3439
end
40+
41+
Given /action mailbox is available/ do
42+
unless RSpec::Rails::FeatureCheck.has_action_mailbox?
43+
pending "Action Mailbox is not available"
44+
end
45+
end

0 commit comments

Comments
 (0)