Skip to content

Commit

Permalink
add an_attachment_with_mime_type option to check mime_type of attac…
Browse files Browse the repository at this point in the history
…hments (#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
  • Loading branch information
tansaku authored Mar 2, 2020
1 parent 1370aab commit 8fbb17d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 15 additions & 0 deletions lib/mail/matchers/attachment_matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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
4 changes: 4 additions & 0 deletions spec/matchers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8fbb17d

Please sign in to comment.