@@ -16,6 +16,13 @@ def email_with_args(arg1, arg2); end
16
16
ActiveJob ::Base . queue_adapter = :test
17
17
end
18
18
19
+ around do |example |
20
+ original_logger = ActiveJob ::Base . logger
21
+ ActiveJob ::Base . logger = Logger . new ( nil ) # Silence messages "[ActiveJob] Enqueued ...".
22
+ example . run
23
+ ActiveJob ::Base . logger = original_logger
24
+ end
25
+
19
26
describe "have_enqueued_mail" do
20
27
it "passes when a mailer method is called with deliver_later" do
21
28
expect {
@@ -42,16 +49,66 @@ def email_with_args(arg1, arg2); end
42
49
end
43
50
44
51
it "passes when negated" do
45
- expect { } . not_to have_enqueued_email ( TestMailer , :test_email )
52
+ expect { } . not_to have_enqueued_mail ( TestMailer , :test_email )
53
+ end
54
+
55
+ it "counts only emails enqueued in the block" do
56
+ TestMailer . test_email . deliver_later
57
+
58
+ expect {
59
+ TestMailer . test_email . deliver_later
60
+ } . to have_enqueued_mail ( TestMailer , :test_email ) . once
46
61
end
47
62
48
- # it "counts only emails enqueued in the block" do
49
- # TestMailer.test_email
63
+ it "fails when too many emails are enqueued" do
64
+ expect {
65
+ expect {
66
+ TestMailer . test_email . deliver_later
67
+ TestMailer . test_email . deliver_later
68
+ } . to have_enqueued_mail ( TestMailer , :test_email ) . exactly ( 1 )
69
+ } . to raise_error ( /expected to enqueue TestMailer.test_email exactly 1 time/ )
70
+ end
50
71
51
- # expect {
52
- # TestMailer.test_email
53
- # }.to have_enqueued_email(TestMailer, :test_email).once
54
- # end
72
+ it "passes with :once count" do
73
+ expect {
74
+ TestMailer . test_email . deliver_later
75
+ } . to have_enqueued_mail ( TestMailer , :test_email ) . once
76
+ end
77
+
78
+ it "passes with :twice count" do
79
+ expect {
80
+ TestMailer . test_email . deliver_later
81
+ TestMailer . test_email . deliver_later
82
+ } . to have_enqueued_mail ( TestMailer , :test_email ) . twice
83
+ end
84
+
85
+ it "passes with :thrice count" do
86
+ expect {
87
+ TestMailer . test_email . deliver_later
88
+ TestMailer . test_email . deliver_later
89
+ TestMailer . test_email . deliver_later
90
+ } . to have_enqueued_mail ( TestMailer , :test_email ) . thrice
91
+ end
92
+
93
+ it "matches based on mailer class and method name" do
94
+ expect {
95
+ TestMailer . test_email . deliver_later
96
+ TestMailer . email_with_args ( 1 , 2 ) . deliver_later
97
+ } . to have_enqueued_mail ( TestMailer , :test_email ) . once
98
+ end
99
+
100
+ it "passes with multiple emails" do
101
+ expect {
102
+ TestMailer . test_email . deliver_later
103
+ TestMailer . email_with_args ( 1 , 2 ) . deliver_later
104
+ } . to have_enqueued_mail ( TestMailer , :test_email ) . and have_enqueued_mail ( TestMailer , :email_with_args )
105
+ end
106
+
107
+ it "passes for mailer methods that accept arguments when the provided argument matcher is not used" do
108
+ expect {
109
+ TestMailer . email_with_args ( 1 , 2 ) . deliver_later
110
+ } . to have_enqueued_mail ( TestMailer , :email_with_args )
111
+ end
55
112
56
113
it "passes with provided argument matchers" do
57
114
expect {
@@ -72,15 +129,15 @@ def email_with_args(arg1, arg2); end
72
129
it "generates a failure message with arguments" do
73
130
expect {
74
131
expect { } . to have_enqueued_email ( TestMailer , :email_with_args ) . with ( 1 , 2 )
75
- } . to raise_error ( /expected to enqueue TestMailer.email_with_args with \[ 1, 2\] / )
132
+ } . to raise_error ( /expected to enqueue TestMailer.email_with_args exactly 1 time with \[ 1, 2\] / )
76
133
end
77
134
78
135
it "throws descriptive error when no test adapter set" do
79
136
queue_adapter = ActiveJob ::Base . queue_adapter
80
137
ActiveJob ::Base . queue_adapter = :inline
81
138
82
139
expect {
83
- expect { TestMailer . test_email } . to have_enqueued_mail ( TestMailer , :test_email )
140
+ expect { TestMailer . test_email . deliver_later } . to have_enqueued_mail ( TestMailer , :test_email )
84
141
} . to raise_error ( "To use ActiveJob matchers set `ActiveJob::Base.queue_adapter = :test`" )
85
142
86
143
ActiveJob ::Base . queue_adapter = queue_adapter
0 commit comments