Programing

Factory Girl을 사용하여 클립 첨부 파일을 생성하는 방법

crosscheck 2020. 7. 17. 19:17
반응형

Factory Girl을 사용하여 클립 첨부 파일을 생성하는 방법


이미지가 많은 모델 Person이 있는데 이미지에는 data라는 클립 첨부 파일 필드가 있고 아래에 약어 버전이 있습니다.

class Person
  has_many :images
  ...
end

class Image
  has_attached_file :data
  belongs_to :person
  ...
end

개인은 하나 이상의 이미지를 첨부해야합니다.

FactoryGirl을 사용할 때 다음과 유사한 코드가 있습니다.

Factory.define :image do |a|
  a.data { File.new(File.join(Rails.root, 'features', 'support', 'file.png')) }
  a.association :person
end

Factory.define :person do |p|
  p.first_name 'Keyzer'
  p.last_name 'Soze'
  p.after_create do |person|
    person.assets = [Factory.build(:image, :person => person)]
  end
  # p.images {|images| [images.association(:image)]}
end

(NB는 위에서 언급 한 코드도 시도했습니다.) 오이 기능을 실행할 때 대부분 다음과 유사한 오류가 발생합니다.

해당 파일 또는 디렉토리가 없습니다-/tmp/stream,9887,0.png (Errno :: ENOENT)

...

때때로 테스트가 성공적으로 실행됩니다.

누구든지 내가 여기서 겪고있는 문제 또는 FactoryGirl과 Paperclip을 함께 사용하여 달성하려는 것과 같은 것을 달성하는 방법을 말해 줄 수 있습니까?

Rails 3을 사용하고 있습니다.


fixture_file_upload 를 사용할 수 있습니다

include ActionDispatch::TestProcess 테스트 도우미에서 다음은 팩토리 예제입니다.

include ActionDispatch::TestProcess

FactoryBot.define do
  factory :user do
    avatar { fixture_file_upload(Rails.root.join('spec', 'photos', 'test.png'), 'image/png') }
  end
end

위의 예제에서 spec/photos/test.png테스트를 실행하기 전에 응용 프로그램의 루트 디렉토리에 있어야합니다.

참고 FactoryBot새 이름입니다 FactoryGirl.


최신 FG 구문 및 포함 할 필요 없음

factory :user do
  avatar { File.new(Rails.root.join('app', 'assets', 'images', 'rails.png')) }
end

더 나은 아직

factory :user do
  avatar { File.new("#{Rails.root}/spec/support/fixtures/image.jpg") } 
end

Pivotal Labs의 Desmond Bowe 메모리 누수 문제로 인해 fixture_file_upload를 피할 것을 제안 합니다. 대신 공장에서 클립 필드를 직접 설정해야합니다.

factory :attachment do
  supporting_documentation_file_name { 'test.pdf' }
  supporting_documentation_content_type { 'application/pdf' }
  supporting_documentation_file_size { 1024 }
  # ...
end

아래 요점에서 코드를 사용하고 있습니다.

레일 2

http://gist.github.com/162881

레일 3

https://gist.github.com/313121


file { File.new(Rails.root.join('spec', 'fixtures', 'filename.png')) }

ActionController :: TestUploadedFile을 사용해보십시오. 파일 속성을 TestUploadedFile의 인스턴스로 설정하면 클립이 나머지를 처리해야합니다. 예를 들어

valid_file = File.new(File.join(Rails.root, 'features', 'support', 'file.png'))  
p.images { 
   [
     ActionController::TestUploadedFile.new(valid_file, Mime::Type.new('application/png'))
   ] 
}

The above answers in some cases can help, and the one actually helped in one of my situations, but when using a Carrierwave, the previous solution from this question didn't work out this time.

FIRST APPROACH:

For me adding an after :create solved the problem for me like this:

after :create do |b|
  b.update_column(:video_file, File.join(Rails.root, 'spec', 'fixtures', 'sample.mp4'))
end

Setting inline video file like video_file { File.new("#{Rails.root}/spec/fixtures/sample.mp4") } didn't work out and it was reporting errors.

SECOND APPROACH:

Define a factory like this (change personal_file to your attachment name):

FactoryGirl.define do
  factory :contact do
    personal_file { File.new("#{Rails.root}/spec/fixtures/personal_files/my_test_file.csv") }
    personal_file_content_type 'text/csv'

  end
end

And add these lines to theconfig/environemnts/test.rb :

config.paperclip_defaults = {
  url: "#{Rails.root}/spec/fixtures/:attachment/:filename",
  use_timestamp: false
}

What are you testing exactly? That paperclip will successfully attach the file? That really seems like a test that paperclip should handle, not your application.

Have you tried

a.data { File.join(Rails.root, 'features', 'support', 'file.png') }

We use Machinist instead of factory_girl and have just used things like

Image.blueprint do
  image { RAILS_ROOT + 'spec/fixtures/images/001.jpg' }
end

Though, we aren't really testing much when we do this, we typically just want to have a valid Image object.

참고URL : https://stackoverflow.com/questions/3294824/how-do-i-use-factory-girl-to-generate-a-paperclip-attachment

반응형