2011-10-08 41 views
19

我正在制作一个rails 3.1应用程序,使用carrierwave将文件上传到aws s3。我遵循了carrierwave github存储库中的说明,现在可以将文件上传到我的存储桶中。这是让我卡住的测试。在过去的两天里,我一直在搜索和修改,使用我发现的所有其他Q & A,但最后我哭了'妈妈'。下面是我得到了什么:rspec测试carrierwave - 新手

/app/uploaders/image_file_uploader.rb

storage :fog 

def store_dir 
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 
end 

/config/initializers/carrierwave.rb

if Rails.env.test? or Rails.env.cucumber? 
    CarrierWave.configure do |config| 
    config.storage = :file 
    config.enable_processing = false 
    end 
end 

/spec/uploaders/image_file_uploader_spec.rb

require 'spec_helper' 
require 'support/fog_helper' 
require 'carrierwave/test/matchers' 

describe ImageFileUploader do 
    include CarrierWave::Test::Matchers 

    before do 
    ImageFileUploader.enable_processing = true 
    @user = Factory(:user, :email => "[email protected]") 
    @uploader = ImageFileUploader.new(@user, Factory(:image)) 
    @uploader.store!(File.open("#{Rails.root}/tmp/uploads/#{Rails.env}/images/")) 
    end 

    after do 
    @uploader.remove! 
    ImageFileUploader.enable_processing = false 
    end 

    context 'the tiny version' do 
    it "should scale down a landscape image to be exactly 50 by 50 pixels" do 
     @uploader.tiny.should have_dimensions(50, 50) 
    end 
    end 

spec/factories.rb

Factory.define :image do |image| 
    include ActionDispatch::TestProcess 

    image.date_taken   "Sun, 09 Oct 2011" 
    image.time_taken   "2000-01-01 03:41:00 UTC" 
    image.image_file   fixture_file_upload('spec/support/test_images/audi.png', 'image/png') 
    image.taken_by    "John Doe" 
    image.collection   "N/A" 
    image.comments    "Beautiful day!" 
    image.association :user 

end 

虽然我/公/上传的/ tmp /是越来越有我测试,其产生的图像的“微小”(和其他版本)填满,测试继续失败,出现以下错误信息:

1)ImageFileUploader微小的版本应该缩小风景图像是完全50 x 50像素

Failure/Error: @uploader = ImageFileUploader.new(@user, Factory(:image)) 
Excon::Errors::NotFound: 
    Expected(200) <=> Actual(404 Not Found) 
    request => {:expects=>200} 
    response => #<Excon::Response:0x0000010569f928 @body="", @headers={}, @status=404> 
# ./spec/uploaders/image_file_uploader_spec.rb:11:in `block (2 levels) in <top (required)>' 

我知道上面的意思是rspec的是没有找到我的测试桶。任何人有任何想法我做错了什么?

将超级感谢任何新的线索。

更新:11年10月11日 文件上传工作,但我搞定了如何让图像通过测试。在短期内,我将使用占位符图像来充实我的应用程序的其余部分,并在稍后返回。一旦我弄清楚了,我会再发布一次更新。 (不过,如果您有任何见解,请留下任何想法。)

+0

你的'@ uploader.store!'应该存放一个目录吗?我会认为这应该是一个特定的文件。 – Andrew

+0

@BenU我的答案解决了你的问题?请接受它,如果是这样的话:-) –

回答

15

您是否试过这个?

  1. /app/uploaders/image_file_uploader.rb删除storage :fog

  2. /config/initializers/carrierwave.rb添加了类似的配置块生产为你的测试和黄瓜,并设置config.storage = :fog那里。

+0

这解决了一个相关的问题,由于上传器的具体配置,而不是根据环境设置我的'存储'配置。在我的情况下,我所有的上传文件都是S3,所以我不需要将存储配置放入上传模型本身。谢谢! – jefflunt