Ruby   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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

@H_267_2@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.

大佬总结

以上是大佬教程为你收集整理的ruby-on-rails – 用Capybara和Rspec测试载波文件上传到s3全部内容,希望文章能够帮你解决ruby-on-rails – 用Capybara和Rspec测试载波文件上传到s3所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。