使用FactoryGirl时,Paperclip :: Attachment作为字符串传递

我正在尝试使用FactoryGirl 3.0在rails 3.2.6上创建回形针3.1.2的function测试,当我传递文件附件时,我的控制器接收一个字符串,它是文件位置而不是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" 

我目前的解决方法是在测试中创建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。

你有没有试过更换

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

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

在你的工厂里面。