diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index 9d2a0dd97..af8686cb9 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -16,5 +16,6 @@ Compatibility: Features: * Message#inspect_structure and PartsList#inspect_structure pretty-print the hierarchy of message parts. (TylerRick) +* `an_attachment_with_mime_type` matcher added to match attachments by mime type Please check [2-7-stable](https://github.com/mikel/mail/blob/2-7-stable/CHANGELOG.rdoc) for previous changes. diff --git a/README.md b/README.md index 169134979..5c57055c0 100644 --- a/README.md +++ b/README.md @@ -645,6 +645,12 @@ describe "sending an email" do # ... or any attachment it { is_expected.to have_sent_email.with_attachments(any_attachment) } + # ... or attachment with filename + it { is_expected.to have_sent_email.with_attachments(an_attachment_with_filename('file.txt')) } + + # ... or attachment with mime_type + it { is_expected.to have_sent_email.with_attachments(an_attachment_with_mime_type('application/pdf')) } + # ... by array of attachments it { is_expected.to have_sent_email.with_attachments([my_attachment1, my_attachment2]) } #note that order is important diff --git a/lib/mail/matchers/attachment_matchers.rb b/lib/mail/matchers/attachment_matchers.rb index 62d0c2ff5..448024abd 100644 --- a/lib/mail/matchers/attachment_matchers.rb +++ b/lib/mail/matchers/attachment_matchers.rb @@ -9,6 +9,10 @@ def an_attachment_with_filename(filename) AttachmentFilenameMatcher.new(filename) end + def an_attachment_with_mime_type(filename) + AttachmentMimeTypeMatcher.new(filename) + end + class AnyAttachmentMatcher def ===(other) other.attachment? @@ -25,5 +29,16 @@ def ===(other) other.attachment? && other.filename == filename end end + + class AttachmentMimeTypeMatcher + attr_reader :mime_type + def initialize(mime_type) + @mime_type = mime_type + end + + def ===(other) + other.attachment? && other.mime_type == mime_type + end + end end end diff --git a/spec/matchers_spec.rb b/spec/matchers_spec.rb index 77c6b497a..1f7b32a16 100644 --- a/spec/matchers_spec.rb +++ b/spec/matchers_spec.rb @@ -176,6 +176,10 @@ it { is_expected.to have_sent_email.with_attachments(an_attachment_with_filename(first_attachment.filename)) } end + context 'matching by mimetype' do + it { is_expected.to have_sent_email.with_attachments(an_attachment_with_mime_type(first_attachment.mime_type)) } + end + context 'single attachment passed' do it { is_expected.to have_sent_email.with_attachments(first_attachment) } end