11

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

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

当我在浏览器中对其进行测试时,它工作正常,但是有关 s3 上传的信息确实被添加到了 url ( screenshot )。不知道为什么这在测试中不起作用。

以下是一些相关文件:

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

初始化程序/carrierwave.rb - https://gist.github.com/leemcalilly/924e8755f7c76ecbf5cf

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

控制器/works_controller.rb - https://gist.github.com/leemcalilly/7fca5f2c81c6cb4de6bc

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

4

1 回答 1

16

好的,我已经想通了。关键是 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

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

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

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

于 2013-06-10T14:04:01.480 回答