forked from mikel/mail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatchers_spec.rb
130 lines (107 loc) · 3.94 KB
/
matchers_spec.rb
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
require 'spec_helper'
describe "have_sent_email" do
include Mail::Matchers
def send_test_email
Mail.deliver do
from '[email protected]'
to ['[email protected]', '[email protected]']
subject 'The facts you requested'
body 'Here are the facts you requested. One-onethousand, two-onethousand.'
end
end
before(:all) do
$old_delivery_method = Mail.delivery_method
Mail.defaults do
delivery_method :test
end
end
after(:all) do
# Although this breaks encapsulation, it's the easiest way to ensure
# that the delivery method is _exactly_ what it was before we started
# messing with it.
Mail::Configuration.instance.instance_variable_set(:@delivery_method, $old_delivery_method)
end
context "without any modifiers" do
context "when no e-mail has been sent" do
before(:each) do
Mail::TestMailer.deliveries.clear
Mail::TestMailer.deliveries.should be_empty
end
it { should_not have_sent_email }
end
context "when e-mail has been sent" do
before(:each) do
send_test_email
Mail::TestMailer.deliveries.should_not be_empty
end
it { should have_sent_email }
end
end
context "with #from" do
context "and a matching sender" do
it { should have_sent_email.from('[email protected]') }
end
context "and a non-matching sender" do
it { should_not have_sent_email.from('[email protected]') }
end
end
context "with #to" do
context "and a matching recipient" do
it { should have_sent_email.to('[email protected]') }
it { should have_sent_email.to('[email protected]') }
it { should have_sent_email.to('[email protected]').to('[email protected]') }
it { should have_sent_email.to(['[email protected]', '[email protected]']) }
end
context "and a non-matching recipient" do
it { should_not have_sent_email.to('[email protected]') }
end
end
context "with #subject" do
context "and a matching subject" do
it { should have_sent_email.with_subject('The facts you requested') }
end
context "and a non-matching subject" do
it { should_not have_sent_email.with_subject('facts you requested') }
it { should_not have_sent_email.with_subject('the facts you') }
it { should_not have_sent_email.with_subject('outright lies') }
end
end
context "with #subject_matching" do
context "and a matching subject" do
it { should have_sent_email.matching_subject(/(facts|fiction) you requested/) }
end
context "and a non-matching subject" do
it { should_not have_sent_email.matching_subject(/The \d+ facts you requested/) }
end
end
context "with #with_body" do
context "and a matching body" do
it { should have_sent_email.with_body('Here are the facts you requested. One-onethousand, two-onethousand.') }
end
context "and a non-matching body" do
it { should_not have_sent_email.with_body('Here are the facts you requested.') }
it { should_not have_sent_email.with_body('are the facts you requested. One-onethousand') }
it { should_not have_sent_email.with_body('Be kind to your web-footed friends, for a duck may be somebody\'s mother') }
end
end
context "with #matching_body" do
context "and a matching body" do
it { should have_sent_email.matching_body(/one-?one(hundred|thousand)/i) }
end
context "and a non-matching body" do
it { should_not have_sent_email.matching_body(/\d+-onethousand/) }
end
end
context "with a huge chain of modifiers" do
it do
should have_sent_email.
from('[email protected]').
to('[email protected]').
to('[email protected]').
with_subject('The facts you requested').
matching_subject(/facts (I|you)/).
with_body('Here are the facts you requested. One-onethousand, two-onethousand.').
matching_body(/(I|you) request/)
end
end
end