我有麻烦测试这个.我可以附加一个文件与水豚,但当我点击按钮上传它不会重定向到正确的行动.我用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来测试这种形式?
解决方法
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