使用Rspec 3查找上传的文件

时间:2014-10-10 07:48:00

标签: ruby-on-rails ruby ruby-on-rails-4 rspec carrierwave

我正在尝试找到我上次上传的文件(使用Carrierwave)并使用Rspec运行have_dimensions方法。

目前我的测试导致失败,错误是

 Failure/Error: expect(@uploader.large_animal_image).to have_dimensions(555, 365)
 Errno::ENOENT:
   No such file or directory @ rb_sysopen - 

我的测试看起来像这样(尝试按照github页面上的示例,但文件上传没有按预期工作)

require 'rails_helper'
require 'carrierwave/test/matchers'

describe AnimalImage do 
  include CarrierWave::Test::Matchers

  before(:each) do
   AnimalImageUploader.enable_processing = true
    @animal = FactoryGirl.create(:image)
    @uploader = AnimalImageUploader.new(@animal, :image)
    ap(@uploader)
    @uploader.store!
  end

  after(:each) do
   @uploader.remove!
   AnimalImageUploader.enable_processing = false
 end

 context 'Image Versions' do

  it 'should scale large_animal_image to 555 x 365 ' do
   expect(@uploader.large_animal_image).to have_dimensions(555, 365)
  end
 end    
end

ap(@uploader)输出

#<AnimalImageUploader:0x000000080ae738 @model=#<AnimalImage id: 50, animal_id: nil, image: "yp2.jpg", created_at: "2014-10-10 07:41:20", updated_at: "2014-10-10 07:41:20">, @mounted_as=:image>

它会创建以下文件

/support
  /animal_image
   /image
    /50
     yp2.jpg
     large_animal_image_yp2.jpg

如何访问这些文件以对其运行have_dimensions方法?

由于

1 个答案:

答案 0 :(得分:1)

好的,通过搞清楚@uploader.store!做什么(愚蠢的我)来解决这个问题

我需要存储我的文件

before(:each) do
 AnimalImageUploader.enable_processing = true
 file = File.open("#{Rails.root}/spec/fixtures/yp2.jpg")
 @animal = AnimalImage.create!(image: file)
 @uploader = AnimalImageUploader.new(@animal, :image)
 @uploader.store!(file)
end