File: matchers_spec.rb

package info (click to toggle)
ruby-mail 2.6.4%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,256 kB
  • ctags: 1,327
  • sloc: ruby: 44,678; makefile: 3
file content (225 lines) | stat: -rw-r--r-- 7,866 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# frozen_string_literal: true
require 'spec_helper'

describe "have_sent_email" do
  include Mail::Matchers

  let(:test_mail) do
    mail = Mail.new(
      :from    =>  'phil@example.com',
      :to      =>  ['bob@example.com', 'fred@example.com'],
      :cc      =>  ['dad@example.com', 'mom@example.com'],
      :bcc     =>  ['alice@example.com', 'sue@example.com'],
      :subject => 'The facts you requested',
      :body    => 'Here are the facts you requested. One-onethousand, two-onethousand.'
    )
    if include_attachments
      mail.attachments['myfile.pdf'] =   { :mime_type => 'application/x-pdf',
                                          :content   => 'test content' }
      mail.attachments['yourfile.csv'] = { :mime_type => 'application/csv',
                                           :content   => '1,2,3' }
    end
    mail
  end

  let(:include_attachments) { true }

  before(:all) do
    $old_delivery_method = Mail.delivery_method

    Mail.defaults do
      delivery_method :test
    end
  end

  before(:each) do
    Mail::TestMailer.deliveries.clear
    test_mail.deliver
  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
        expect(Mail::TestMailer.deliveries).to be_empty
      end

      it { is_expected.not_to have_sent_email }
    end

    context "when e-mail has been sent" do
      before(:each) do
        expect(Mail::TestMailer.deliveries).not_to be_empty
      end

      it { is_expected.to have_sent_email }
    end
  end

  context "with #from" do
    context "and a matching sender" do
      it { is_expected.to have_sent_email.from('phil@example.com') }
    end

    context "and a non-matching sender" do
      it { is_expected.not_to have_sent_email.from('sven@example.com') }
    end
  end

  context "with #to" do
    context "and a matching recipient" do
      it { is_expected.to have_sent_email.to('bob@example.com') }
      it { is_expected.to have_sent_email.to('fred@example.com') }
      it { is_expected.to have_sent_email.to('bob@example.com').to('fred@example.com') }
      it { is_expected.to have_sent_email.to(['bob@example.com', 'fred@example.com']) }
    end

    context "and a non-matching recipient" do
      it { is_expected.not_to have_sent_email.to('sven@example.com') }
    end
  end

  context "with #cc" do
    context "and a matching recipient" do
      it { is_expected.to have_sent_email.cc('mom@example.com') }
      it { is_expected.to have_sent_email.cc('dad@example.com') }
      it { is_expected.to have_sent_email.cc('mom@example.com').cc('dad@example.com') }
      it { is_expected.to have_sent_email.cc(['mom@example.com', 'dad@example.com']) }
    end

    context "and a non-matching recipient" do
      it { is_expected.not_to have_sent_email.cc('granny@example.com') }
    end
  end

  context "with #bcc" do
    context "and a matching recipient" do
      it { is_expected.to have_sent_email.bcc('alice@example.com') }
      it { is_expected.to have_sent_email.bcc('sue@example.com') }
      it { is_expected.to have_sent_email.bcc('alice@example.com').bcc('sue@example.com') }
      it { is_expected.to have_sent_email.bcc(['alice@example.com', 'sue@example.com']) }
    end

    context "and a non-matching recipient" do
      it { is_expected.not_to have_sent_email.bcc('mario@example.com') }
    end
  end

  context "with #subject" do
    context "and a matching subject" do
      it { is_expected.to have_sent_email.with_subject('The facts you requested') }
    end

    context "and a non-matching subject" do
      it { is_expected.not_to have_sent_email.with_subject('facts you requested') }
      it { is_expected.not_to have_sent_email.with_subject('the facts you') }
      it { is_expected.not_to have_sent_email.with_subject('outright lies') }
    end
  end

  context "with #subject_matching" do
    context "and a matching subject" do
      it { is_expected.to have_sent_email.matching_subject(/(facts|fiction) you requested/) }
    end

    context "and a non-matching subject" do
      it { is_expected.not_to have_sent_email.matching_subject(/The \d+ facts you requested/) }
    end
  end

  context "with #with_body" do
    context "and a matching body" do
      it { is_expected.to have_sent_email.with_body('Here are the facts you requested. One-onethousand, two-onethousand.') }
    end

    context "and a non-matching body" do
      it { is_expected.not_to have_sent_email.with_body('Here are the facts you requested.') }
      it { is_expected.not_to have_sent_email.with_body('are the facts you requested. One-onethousand') }
      it { is_expected.not_to have_sent_email.with_body('Be kind to your web-footed friends, for a duck may be somebody\'s mother') }
    end
  end

  context 'with #with_attachments' do
    let(:first_attachment) { test_mail.attachments.first }
    let(:last_attachment) { test_mail.attachments.last }

    context 'and matching attachments' do

      context 'matching by filename' do
        it { is_expected.to have_sent_email.with_attachments(an_attachment_with_filename(first_attachment.filename)) }
      end

      context 'single attachment passed' do
        it { is_expected.to have_sent_email.with_attachments(first_attachment) }
      end

      context 'array of attachments passed' do
        it {is_expected.to have_sent_email.with_attachments([first_attachment, last_attachment]) }
      end

      context 'any_attachment passed' do
        it {is_expected.to have_sent_email.with_attachments([any_attachment]) }
      end

      context 'chaining attachment matching' do
        it { is_expected.to have_sent_email.with_attachments(first_attachment).with_attachments([last_attachment]) }
      end

      context 'ce matching' do
        it { is_expected.to have_sent_email.with_attachments(first_attachment).with_attachments([last_attachment]) }
      end

      context 'attachment order is important' do
        it {is_expected.to have_sent_email.with_attachments([first_attachment, last_attachment]) }
        it {is_expected.to_not have_sent_email.with_attachments([last_attachment, first_attachment]) }
      end
    end

    context 'and non-matching attachments' do
      it { is_expected.not_to have_sent_email.with_attachments('no_match') }
      it { is_expected.not_to have_sent_email.with_attachments(an_attachment_with_filename('no_match')) }
    end

    context 'and any attachments' do
      it { is_expected.to have_sent_email.with_any_attachments }
    end

    context 'and no attachments' do
      let(:include_attachments) { false }
      it { is_expected.to have_sent_email.with_no_attachments }
    end
  end

  context "with #matching_body" do
    context "and a matching body" do
      it { is_expected.to have_sent_email.matching_body(/one-?one(hundred|thousand)/i) }
    end

    context "and a non-matching body" do
      it { is_expected.not_to have_sent_email.matching_body(/\d+-onethousand/) }
    end
  end

  context "with a huge chain of modifiers" do
    it do
      is_expected.to have_sent_email.
             from('phil@example.com').
             to('bob@example.com').
             to('fred@example.com').
             with_subject('The facts you requested').
             with_attachments(test_mail.attachments.first).
             matching_subject(/facts (I|you)/).
             with_body('Here are the facts you requested. One-onethousand, two-onethousand.').
             matching_body(/(I|you) request/).
             with_any_attachments
    end
  end
end