测试来自载波上传器的邮件附件

时间:2019-03-28 09:46:24

标签: ruby-on-rails email rspec carrierwave email-attachments

我不明白如何编写一个可以对模型载波上传器进行存根的规范,以便实际上可以在邮件规范中对其进行测试

class Message
  mount_uploader :attachment, ::AttachmentUploader
end

class MessageMailer
  def new_message(message
    ...
    attachments[message.attachment.file.filename] = message.attachment.read
  end
end

我已经尝试了几种方法,看来我无法使用Rspec allow用存根替换我的mount_uploader,所以我求助于实际在其中发送实际文件的过程,但是在测试模式下,似乎载波实际上并没有存储文件,结果为

  

undefined method length' for <CarrierWave::SanitizedFile:0x007fc89c0095d0>

这是来自mailer gem:

0: /Users/Cyril/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/mail-2.7.0/lib/mail/message.rb:2011:in `body_lazy'
 1: /Users/Cyril/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/mail-2.7.0/lib/mail/message.rb:1245:in `body='
 2: /Users/Cyril/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/mail-2.7.0/lib/mail/message.rb:2124:in `init_with_hash'
 3: /Users/Cyril/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/mail-2.7.0/lib/mail/message.rb:135:in `initialize'
 4: /Users/Cyril/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/mail-2.7.0/lib/mail/attachments_list.rb:83:in `new'
 5: /Users/Cyril/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/mail-2.7.0/lib/mail/attachments_list.rb:83:in `[]='
[next line is my mailer code that adds the attachment]

任何有关如何使用carrierwave mount_uploader测试邮件的帮助/准则都将受到赞赏

shared_examples 'a mailer action that sends the message attachment' do
  before do
    attachment = File.new(file_fixture('avatar.png'))
    message.attachment = attachment
    message.save
  end

  it 'sends the message attachment' do
    expect(mail.attachments.first.present?).to be
  end
end

0 个答案:

没有答案