Skip to content

Commit 8fbb17d

Browse files
authored
add an_attachment_with_mime_type option to check mime_type of attachments (#1380)
* adding support for a mime_type attachment matcher * add test for new mime_type attachment checker * update README and CHANGELOG for an_attachment_with_mime_type
1 parent 1370aab commit 8fbb17d

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

CHANGELOG.rdoc

+1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ Compatibility:
1616

1717
Features:
1818
* Message#inspect_structure and PartsList#inspect_structure pretty-print the hierarchy of message parts. (TylerRick)
19+
* `an_attachment_with_mime_type` matcher added to match attachments by mime type
1920

2021
Please check [2-7-stable](https://github.com/mikel/mail/blob/2-7-stable/CHANGELOG.rdoc) for previous changes.

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,12 @@ describe "sending an email" do
645645
# ... or any attachment
646646
it { is_expected.to have_sent_email.with_attachments(any_attachment) }
647647

648+
# ... or attachment with filename
649+
it { is_expected.to have_sent_email.with_attachments(an_attachment_with_filename('file.txt')) }
650+
651+
# ... or attachment with mime_type
652+
it { is_expected.to have_sent_email.with_attachments(an_attachment_with_mime_type('application/pdf')) }
653+
648654
# ... by array of attachments
649655
it { is_expected.to have_sent_email.with_attachments([my_attachment1, my_attachment2]) } #note that order is important
650656

lib/mail/matchers/attachment_matchers.rb

+15
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ def an_attachment_with_filename(filename)
99
AttachmentFilenameMatcher.new(filename)
1010
end
1111

12+
def an_attachment_with_mime_type(filename)
13+
AttachmentMimeTypeMatcher.new(filename)
14+
end
15+
1216
class AnyAttachmentMatcher
1317
def ===(other)
1418
other.attachment?
@@ -25,5 +29,16 @@ def ===(other)
2529
other.attachment? && other.filename == filename
2630
end
2731
end
32+
33+
class AttachmentMimeTypeMatcher
34+
attr_reader :mime_type
35+
def initialize(mime_type)
36+
@mime_type = mime_type
37+
end
38+
39+
def ===(other)
40+
other.attachment? && other.mime_type == mime_type
41+
end
42+
end
2843
end
2944
end

spec/matchers_spec.rb

+4
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@
176176
it { is_expected.to have_sent_email.with_attachments(an_attachment_with_filename(first_attachment.filename)) }
177177
end
178178

179+
context 'matching by mimetype' do
180+
it { is_expected.to have_sent_email.with_attachments(an_attachment_with_mime_type(first_attachment.mime_type)) }
181+
end
182+
179183
context 'single attachment passed' do
180184
it { is_expected.to have_sent_email.with_attachments(first_attachment) }
181185
end

0 commit comments

Comments
 (0)