使用Capybara和Rspec测试Carrierwave文件上传到s3

Lee*_*lly 10 rspec ruby-on-rails amazon-s3 capybara carrierwave

我已经将载波文件上传到亚马逊s3,就像在这个Railscast中一样.

我在测试时遇到了麻烦.我可以附加Capybara的文件,但是当我点击按钮上传时,它不会重定向到正确的操作.我查看了save_and_open_page,它正在显示主页.

当我在浏览器中测试它时工作正常,但有关s3上传的信息确实会添加到网址(屏幕截图).不知道为什么这在测试中不起作用.

以下是一些相关文件:

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测试这种类型的表格?

Lee*_*lly 15

好的,我已经弄明白了.关键是CarrierWaveDirect:

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

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

include CarrierWaveDirect::Test::CapybaraHelpers

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

attach_file_for_direct_upload("#{Rails.root}/spec/support/images/example.jpg") upload_directly(ImageUploader.new, "Upload Image")

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

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
Run Code Online (Sandbox Code Playgroud)

我还需要将它添加到我的initializers/carrierwave.rb中:

if Rails.env.test?
    CarrierWave.configure do |config|
      config.storage = :file
      config.enable_processing = false
    end
end
Run Code Online (Sandbox Code Playgroud)

我没有模拟对雾的响应,也没有测试上传到s3,而是在测试环境中关闭了上传到s3.