Ruby-on-rails 如何使用 Factory Girl 生成回形针附件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3294824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How Do I Use Factory Girl To Generate A Paperclip Attachment?
提问by Laz
I have model Person that has many Images, where images has a Paperclip attachment field called data, an abbreviated version displayed below:
我的模型 Person 有很多图像,其中图像有一个名为 data 的 Paperclip 附件字段,缩写版本如下所示:
class Person
has_many :images
...
end
class Image
has_attached_file :data
belongs_to :person
...
end
Person is required to have at least one Image attached to it.
人必须至少附有一张图片。
When using FactoryGirl, I have code akin to the following:
使用 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
(N.B. I have also tried the code commented out above was also tried) Most of the time when I run cucumber features, I get an error akin to the following:
(注意,我也尝试过上面注释掉的代码也尝试过)大多数时候,当我运行黄瓜功能时,我会收到类似于以下内容的错误:
No such file or directory - /tmp/stream,9887,0.png (Errno::ENOENT)
...
没有这样的文件或目录 - /tmp/stream,9887,0.png (Errno::ENOENT)
...
Sometimes the tests run successfully.
有时测试会成功运行。
Can anyone tell me what the problem is I am having here or how they use FactoryGirl and Paperclip together to achieve something like what I am trying to achieve?
谁能告诉我我在这里遇到了什么问题,或者他们如何一起使用 FactoryGirl 和 Paperclip 来实现我想要实现的目标?
I am using Rails 3.
我正在使用 Rails 3。
回答by DanS
You can use fixture_file_upload
您可以使用fixture_file_upload
include ActionDispatch::TestProcessin your test helper, here is an example factory:
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
In the above example, spec/photos/test.pngneeds to exist in your application's root directory before running your tests.
在上面的例子中,spec/photos/test.png需要在运行测试之前存在于应用程序的根目录中。
Note, that FactoryBotis a new name for FactoryGirl.
请注意,这FactoryBot是FactoryGirl.
回答by Neal
Newer FG syntax and no includes necessary
较新的 FG 语法,不需要包含
factory :user do
avatar { File.new(Rails.root.join('app', 'assets', 'images', 'rails.png')) }
end
or, better yet,
或者,更好的是,
factory :user do
avatar { File.new("#{Rails.root}/spec/support/fixtures/image.jpg") }
end
回答by Rick Quantz
Desmond Bowe over at Pivotal Labs suggests avoiding fixture_file_uploaddue to memory leak problems. Instead, you should set the paperclip fields directly in your factory:
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
回答by deb
I've been using the code in the gist below:
我一直在使用以下要点中的代码:
Rails 2
导轨 2
Rails 3
导轨 3
回答by Matthew Deiters
file { File.new(Rails.root.join('spec', 'fixtures', 'filename.png')) }
回答by britt
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
尝试使用 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'))
]
}
回答by Aleks
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.
在某些情况下,上述答案会有所帮助,而实际上在我的一种情况下有所帮助,但是在使用 Carrierwave 时,此问题的先前解决方案这次没有奏效。
FIRST APPROACH:
第一种方法:
For me adding an after :createsolved the problem for me like this:
对我来说,添加一个after :create解决了我这样的问题:
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.
设置内嵌视频文件之类的video_file { File.new("#{Rails.root}/spec/fixtures/sample.mp4") }不起作用,并且报告错误。
SECOND APPROACH:
第二种方法:
Define a factory like this (change personal_fileto your attachment name):
像这样定义一个工厂(更改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
And add these lines to theconfig/environemnts/test.rb:
并将这些行添加到config/environemnts/test.rb:
config.paperclip_defaults = {
url: "#{Rails.root}/spec/fixtures/:attachment/:filename",
use_timestamp: false
}
回答by theIV
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
我们使用机械师而不是 factory_girl 并且刚刚使用了诸如
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 Imageobject.
虽然,当我们这样做时,我们并没有真正测试太多,我们通常只想拥有一个有效的Image对象。

