'Add attachments with ActionMailbox TestHelper
I want to test attachments with the Action Mailbox TestHelper. I see the options for the test helper get passed to the mail object but I can't seem to add attachments. They end up being nil.
This is what I have:
receive_inbound_email_from_mail(
attachments: [
File.new(File.join(File.dirname(__FILE__), '../support/fixtures/image.jpeg'))
],
from: '[email protected],
to: '[email protected]'
)
How does it work?
Solution 1:[1]
This is supported in Edge but ActionMailbox::TestHelper 6.0.3.2 has not implemented it yet.
As a workaround, I came up with this:
subject do
mail = Mail.new do
to '[email protected]'
from 'Mikel Lindsaar <[email protected]>'
subject 'First multipart email sent with Mail'
text_part do
body 'Here is the attachment you wanted'
end
html_part do
content_type 'text/html; charset=UTF-8'
body '<h1>Funky Title</h1><p>Here is the attachment you wanted</p>'
end
add_file File.join(File.dirname(__FILE__), '../support/fixtures/image.jpeg')
end
# Tap the route for processing.
create_inbound_email_from_source(mail.to_s, status: :processing).tap(&:route)
end
If you're using edge or a newer version this should work:
receive_inbound_email_from_mail do |mail|
mail.to "David Heinemeier Hansson <[email protected]>"
mail.from "Bilbo Baggins <[email protected]>"
mail.subject "Come down to the Shire!"
mail.text_part do |part|
part.body "Please join us for a party at Bag End"
end
mail.html_part do |part|
part.body "<h1>Please join us for a party at Bag End</h1>"
end
mail.add_file File.join(File.dirname(__FILE__), '../support/fixtures/image.jpeg')
end
Note: You'll need to change the file path to suit your needs. And receive_
methods are the same as create_
but they process the mail.
Solution 2:[2]
A complement to Rimian answer, just in case you want to use 'let' to set up your mail var:
let(:mail){
mail = Mail.new
mail.to = '[email protected]'
mail.from = '[email protected]'
mail.subject = 'your subject'
mail.body = 'test body'
mail.add_file File.join(fixture_path, 'folder_to_file/file_name.png')
mail
}
and then
subject{
create_inbound_email_from_source(mail.to_s, status: :processing).tap(&:route)
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Rimian |
Solution 2 | pedromamede |