我有一个名为“Person”的模型,其中包含许多Image对象,每个Image对象都有一个Paperclip附件字段称为“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
(注意:我也尝试了上面注释掉的代码)
大多数情况下,当我运行Cucumber特性时,我会遇到类似于以下的错误:
没有这样的文件或目录-/tmp/stream,9887,0.png(Errno::ENOENT)
...
有时测试可以成功运行。
有人能告诉我这里出了什么问题,或者他们如何一起使用FactoryGirl和Paperclip来实现类似我所尝试的东西吗?
我正在使用Rails 3。