ruby-on-rails – 用Capybara和Rspec测试载波文件上传到s3

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 用Capybara和Rspec测试载波文件上传到s3前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
像在 this Railscast那样,我已经将载波文件上传到了Amazon S3.

我有麻烦测试这个.我可以附加一个文件与水豚,但当我点击按钮上传它不会重定向到正确的行动.我用save_and_open_page检查,而是显示主页.

当我在浏览器中测试它可以正常工作,但s3上传的信息确实添加到url(screenshot).不知道为什么这在测试中不起作用.

以下是一些相关文件

example_spec.rb – https://gist.github.com/leemcalilly/1e159f1b93005b8113f2

initializers / carrierwave.rb – https://gist.github.com/leemcalilly/924e8755f7c76ecbf5cf

models / work.rb – https://gist.github.com/leemcalilly/cfda1a7f15d87dbab731

controllers / works_controller.rb – https://gist.github.com/leemcalilly/7fca5f2c81c6cb4de6bc

我怎么能用capybara和rspec来测试这种形式?

解决方法

好的,我已经弄清楚了.关键是CarrierWaveDirect:

https://github.com/dwilkie/carrierwave_direct#using-capybara

我需要将此行添加到我的spec_helper.rb中:

包括CarrierWaveDirect :: Test :: CapybaraHelpers

然后我的测试需要这些CarrierWaveDirect匹配器:

attach_file_for_direct_upload( “#{Rails.root} /spec/support/images/example.jpg”)
upload_directly(ImageUploader.new,“上传图片”)

所以最后的通过测试看起来像这样:

it "creates a new work with a test image" do
    client = FactoryGirl.create(:client)
    work = FactoryGirl.build(:work)
    visit works_path
    attach_file_for_direct_upload("#{Rails.root}/spec/support/images/example.jpg")
    upload_directly(ImageUploader.new,"Upload Image")
    fill_in "Name",:with => work.name
    select("2012",:from => "work_date_1i")
    select("December",:from => "work_date_2i")
    select("25",:from => "work_date_3i")
    select(client.name,:from => "work_client_ids")
    fill_in "Description",:with => work.description
    fill_in "Service",:with => work.service
    save_and_open_page
    check "Featured"
    click_button "Create Work"
    page.should have_content("Work was successfully created.")
end

我还需要将它添加到我的初始化器/ carrierwave.rb中:

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

而不是模拟对雾的响应,或测试上传到s3,我刚刚在测试环境中关闭上传到s3.

原文链接:https://www.f2er.com/ruby/267150.html

猜你在找的Ruby相关文章