11

この Railscastのように、Carrierwave ファイルの Amazon s3 へのアップロードを実装しました。

私はこれをテストするのに問題があります。Capybara でファイルを添付できますが、ボタンをクリックしてアップロードすると、正しいアクションにリダイレクトされません。save_and_open_page で確認したところ、代わりにホームページが表示されています。

ブラウザでテストすると正常に動作しますが、s3 アップロードに関する情報が URL に追加されます (スクリーンショット)。なぜこれがテストで機能しないのかわかりません。

関連するファイルを次に示します。

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

初期化子/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

このタイプのフォームをカピバラと 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 に答える