119

我的模型 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

(注意我也试过上面注释掉的代码也试过了)大多数时候我运行黄瓜功能时,我会收到类似于以下内容的错误:

没有这样的文件或目录 - /tmp/stream,9887,0.png (Errno::ENOENT)

...

有时测试会成功运行。

谁能告诉我我在这里遇到了什么问题,或者他们如何一起使用 FactoryGirl 和 Paperclip 来实现我想要实现的目标?

我正在使用 Rails 3。

4

8 回答 8

89

您可以使用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

于 2012-04-02T21:06:22.217 回答
49

较新的 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
于 2013-06-28T02:52:13.057 回答
39

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
于 2013-11-21T21:48:05.210 回答
25

我一直在使用以下要点中的代码:

导轨 2

http://gist.github.com/162881

导轨 3

https://gist.github.com/313121

于 2010-08-02T19:09:00.127 回答
12
file { File.new(Rails.root.join('spec', 'fixtures', 'filename.png')) }
于 2010-10-29T06:13:41.073 回答
4

Try using ActionController::TestUploadedFile. You can just set the file property to an instance of TestUploadedFile and paperclip should take care of the rest. For example

valid_file = File.new(File.join(Rails.root, 'features', 'support', 'file.png'))  
p.images { 
   [
     ActionController::TestUploadedFile.new(valid_file, Mime::Type.new('application/png'))
   ] 
}
于 2010-07-22T22:32:17.810 回答
0

在某些情况下,上述答案可能会有所帮助,并且在我的一种情况下实际上有所帮助,但是当使用载波时,这个问题的先前解决方案这次没有解决。

第一种方法:

对我来说,添加一个after :create为我解决了这样的问题:

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

设置内联视频文件video_file { File.new("#{Rails.root}/spec/fixtures/sample.mp4") }没有成功,它正在报告错误。

第二种方法:

像这样定义一个工厂(更改personal_file为您的附件名称):

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

并将这些行添加到config/environemnts/test.rb

config.paperclip_defaults = {
  url: "#{Rails.root}/spec/fixtures/:attachment/:filename",
  use_timestamp: false
}
于 2016-12-29T10:00:43.647 回答
-1

你到底在测试什么?那个回形针会成功附加文件吗?这看起来真的像是回形针应该处理的测试,而不是你的应用程序。

你有没有尝试过

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

我们使用机械师而不是 factory_girl 并且刚刚使用了类似的东西

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

虽然,当我们这样做时,我们并没有真正进行太多测试,但我们通常只想拥有一个有效的Image对象。

于 2010-07-22T23:07:49.890 回答