首页 > 解决方案 > Rspec - 测试 Carrierwave 上传器

问题描述

我需要为我的上传器测试我的自定义fog_public方法。

  def fog_public
    return false if mounted_as == :pdf_file

    super
  end

这个上传器正在多个地方使用,但我只需要在mounted_as == :pdf_file 时覆盖fog_public

这是我的规格文件:

require 'spec_helper'

describe PdfFileUploader do
  include CarrierWave::Test::Matchers
  let(:uploader) { described_class.new(mock_model(Report).as_null_object) }

  before do
    uploader.store!(File.open(Rails.root.join('spec', 'fixtures', 'files', 'testing_sample.pdf')))
  end

  it 'fog_public is false' do
    expect(uploader.fog_public).to eq(false)
  end
end

在此处输入图像描述我的上传器在我的报告模型 中安装为:pdf_file 。由于我无法指定何时mount_as :pdf_file ,因此 fog_public 在规范中返回 true 。关于如何为特定模型指定mounted_as的任何想法?

标签: rspeccarrierwaverspec-railsfog

解决方案


推荐阅读