简体   繁体   中英

Factory_Girl array

How to change the factory:

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

to provide this ( photo is an array ):

{"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>>]},

rather than that:

{"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"}

Use this instead

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM