1

FactoryGirl 3.0 を使用して Rails 3.2.6 で paperclip 3.1.2 の機能テストを作成しようとしています。添付ファイルを渡すと、コントローラーは Paperclip::Attachment オブジェクトではなくファイルの場所である文字列を受け取ります。

私の工場は:

include ActionDispatch::TestProcess
FactoryGirl.define do
   factory :artist do
    id 1
    name 'MyString'
    photo {fixture_file_upload("#{Rails.root}/test/fixtures/files/rails.png",'image/png')}
  end
end

そして私のテストは次のとおりです。

test "should create artist" do
  assert_difference('Artist.count') do
    post :create, :artist => { name: @artist.name, photo: @artist.photo }, :html => { :multipart => true }
  end

  assert_redirected_to artist_path(assigns(:artist))
end

私のコントローラーではこれ:

params[:artist][:photo].class.name 

「文字列」に等しいが、これをテストに追加すると合格する

assert @artist.photo.class.name == "Paperclip::Attachment"

私の現在の回避策は、 test で Fixture_file_upload を作成することです:

test "should create artist" do
  assert_difference('Artist.count') do
    photo = fixture_file_upload("/files/rails.png",'image/png')
    post :create, :artist => { name: @artist.name, photo: photo }, :html => { :multipart => true }
  end

  assert_redirected_to artist_path(assigns(:artist))
end

これは機能しますが、「Rack::Test::UploadedFile」を作成するため、両方を作成するために同じメソッドが呼び出されたときにファクトリが「Paperclip::Attachment」を返す理由がかなり混乱しています。

ファクトリの外部でfixture_file_uploadを定義するのではなく、ファクトリを使用したいので、これに光を当てることができることを事前に感謝します。

4

1 に答える 1

1

交換してみましたか

photo {fixture_file_upload("#{Rails.root}/test/fixtures/files/rails.png",'image/png')}

photo File.new("#{Rails.root}/test/fixtures/files/rails.png")

あなたの工場の中。

于 2012-07-01T17:02:43.047 に答える