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

时间:2021-11-15 23:02:52

I've implemented carrierwave file uploads to Amazon s3 just like in this Railscast.

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

I'm having trouble testing this though. I can attach a file with Capybara, but when I click the button to upload it doesn't redirect to the correct action. I checked with save_and_open_page and it's displaying the the homepage instead.

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

When I test it in the browser it works fine, but the info about the s3 upload does get added to the url (screenshot). Not sure why this wouldn't work in the test.

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

Here are some relevant files:

以下是一些相关文件:

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

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

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

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

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

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

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

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

How can I test this type of form with capybara and rspec?

如何用capybara和rspec测试这种类型的表格?

1 个解决方案

#1


15  

Ok, I've figured this out. The key is CarrierWaveDirect:

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

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

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

I needed to add this line to my spec_helper.rb:

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

include CarrierWaveDirect::Test::CapybaraHelpers

包括CarrierWaveDirect :: Test :: CapybaraHelpers

Then my tests needed these CarrierWaveDirect matchers:

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

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

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

So the final passing test looks like this:

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

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

I also needed to add this to my initializers/carrierwave.rb:

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

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

Rather than mock the response to fog, or test an upload to s3, I just switched off uploads to s3 in the test environment.

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

#1


15  

Ok, I've figured this out. The key is CarrierWaveDirect:

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

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

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

I needed to add this line to my spec_helper.rb:

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

include CarrierWaveDirect::Test::CapybaraHelpers

包括CarrierWaveDirect :: Test :: CapybaraHelpers

Then my tests needed these CarrierWaveDirect matchers:

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

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

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

So the final passing test looks like this:

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

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

I also needed to add this to my initializers/carrierwave.rb:

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

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

Rather than mock the response to fog, or test an upload to s3, I just switched off uploads to s3 in the test environment.

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