imageがない場合
spec/mailers/email_spec.rb
let(:mail) { described_class.send_email(user)}
it 'assigns activation url' do
url = something_url(user)
expect(mail.body.encoded).to have_link(t('activate'), href: url)
end
画像を入れる
app/helpers/email_helper.rb
module EmailHelper
def email_image_tag(image, options = {})
image_tag email_image_url(image), options
end
def email_image_url(image)
attachments[image] = File.read Rails.root.join("app/assets/images/#{image}")
attachments[image].url
end
end
app/views/user_mailer/send_mail.haml
...
= email_image_tag('test.png') # app/assets/image/test.png
...
画像を入れるとContent-typeが変わってしまい、上記のURLのテストがうまくいかない
解決方法
spec/supports/email_helper.rb
module EmailHelper
def get_message_part(mail, content_type)
mail.body.parts.find { |p| p.content_type.match content_type }
.body.raw_source
end
end
spec/mailers/email_spec.rb
include EmailHelper
let(:mail) { described_class.send_email(user)}
let(:msg) { get_message_part mail, /html/ }
it 'assigns activation url' do
url = something_url(user)
expect(msg).to have_link(t('activate'), href: url)
end