Skip to content

Commit 42b8dae

Browse files
author
Joel Lubrano
committed
Add tests for arguments matcher and error messages. Largely based on existing ActiveJob matcher tests.
1 parent e21943d commit 42b8dae

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

spec/rspec/rails/matchers/have_enqueued_mail_spec.rb

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,68 @@ def email_with_args(arg1, arg2); end
2222
TestMailer.test_email.deliver_later
2323
}.to have_enqueued_mail(TestMailer, :test_email)
2424
end
25+
26+
it "passes when using the have_enqueued_email alias" do
27+
expect {
28+
TestMailer.test_email.deliver_later
29+
}.to have_enqueued_email(TestMailer, :test_email)
30+
end
31+
32+
it "passes when using the enqueue_mail alias" do
33+
expect {
34+
TestMailer.test_email.deliver_later
35+
}.to enqueue_mail(TestMailer, :test_email)
36+
end
37+
38+
it "passes when using the enqueue_email alias" do
39+
expect {
40+
TestMailer.test_email.deliver_later
41+
}.to enqueue_email(TestMailer, :test_email)
42+
end
43+
44+
it "passes when negated" do
45+
expect { }.not_to have_enqueued_email(TestMailer, :test_email)
46+
end
47+
48+
# it "counts only emails enqueued in the block" do
49+
# TestMailer.test_email
50+
51+
# expect {
52+
# TestMailer.test_email
53+
# }.to have_enqueued_email(TestMailer, :test_email).once
54+
# end
55+
56+
it "passes with provided argument matchers" do
57+
expect {
58+
TestMailer.email_with_args(1, 2).deliver_later
59+
}.to have_enqueued_mail(TestMailer, :email_with_args).with(1, 2)
60+
61+
expect {
62+
TestMailer.email_with_args(1, 2).deliver_later
63+
}.not_to have_enqueued_mail(TestMailer, :email_with_args).with(3, 4)
64+
end
65+
66+
it "generates a failure message" do
67+
expect {
68+
expect { }.to have_enqueued_email(TestMailer, :test_email)
69+
}.to raise_error(/expected to enqueue TestMailer.test_email/)
70+
end
71+
72+
it "generates a failure message with arguments" do
73+
expect {
74+
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\]/)
76+
end
77+
78+
it "throws descriptive error when no test adapter set" do
79+
queue_adapter = ActiveJob::Base.queue_adapter
80+
ActiveJob::Base.queue_adapter = :inline
81+
82+
expect {
83+
expect { TestMailer.test_email }.to have_enqueued_mail(TestMailer, :test_email)
84+
}.to raise_error("To use ActiveJob matchers set `ActiveJob::Base.queue_adapter = :test`")
85+
86+
ActiveJob::Base.queue_adapter = queue_adapter
87+
end
2588
end
2689
end

0 commit comments

Comments
 (0)