ค้นหาไฟล์ที่อัพโหลดด้วย Rspec 3

ฉันกำลังพยายามค้นหาไฟล์ที่อัปโหลดล่าสุดของฉัน (โดยใช้ Carrierwave) และเรียกใช้เมธอด have_dimensions กับไฟล์นั้นด้วย Rspec

ในขณะนี้ผลการทดสอบของฉันล้มเหลว ข้อผิดพลาดคือ

 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 กับไฟล์เหล่านั้นได้อย่างไร

ขอบคุณ


person Richlewis    schedule 10.10.2014    source แหล่งที่มา


คำตอบ (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
person Richlewis    schedule 10.10.2014