Factory_Girl数组

时间:2014-03-28 22:05:29

标签: ruby-on-rails factory-bot

如何更改工厂:

FactoryGirl.define do

  factory :slide do

    association :album
    description Faker::Name.name    
    photo { fixture_file_upload(Rails.root.join('spec/fixtures/sample_1.jpg'), 'image/jpeg') }

  end
end

提供此功能(照片是数组):

{"slide"=>
  {"description"=>"Rylan Gerhold",
   "photo"=>
    [#<ActionDispatch::Http::UploadedFile:0x00000005720510
      @content_type="image/jpeg",
      @headers=
       "Content-Disposition: form-data; name=\"slide[photo][]\"; filename=\"DSC_2382.JPG\"\r\nContent-Type: image/jpeg\r\n",
      @original_filename="DSC_2382.JPG",
      @tempfile=#<File:/tmp/RackMultipart20140328-1250-l4u0uv>>]},

而不是:

{"slide"=>
  {"description"=>"Rylan Gerhold",
   "photo"=>
    #<Rack::Test::UploadedFile:0x0000000542b8a0
     @content_type="image/jpeg",
     @original_filename="sample_1.jpg",
     @tempfile=#<File:/tmp/sample_1.jpg20140328-32599-lwntv5>>},
 "album_id"=>"1",
 "controller"=>"slides",
 "action"=>"create"}

1 个答案:

答案 0 :(得分:1)

改为使用

FactoryGirl.define do

  factory :slide do

    association :album
    description Faker::Name.name    
    photo [Rack::Test::UploadedFile.new("#{Rails.root}/spec/fixtures/sample_1.jpg", "image/jpeg")]

  end
end