我有模型人物有很多图像,其中图像有一个名为data的Paperclip附件字段,缩写版本如下所示:
class Person
has_many :images
...
end
class Image
has_attached_file :data
belongs_to :person
...
end
Run Code Online (Sandbox Code Playgroud)
人员必须至少附上一张图像.
使用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
Run Code Online (Sandbox Code Playgroud)
(注意,我也尝试过上面注释的代码也尝试了)大多数时候,当我运行黄瓜功能时,我得到的错误类似于以下内容:
没有这样的文件或目录 - /tmp/stream,9887,0.png(Errno :: ENOENT)
...
有时测试成功运行.
谁能告诉我我在这里遇到的问题是什么,或者他们如何一起使用FactoryGirl和Paperclip来实现我想要实现的目标?
我正在使用Rails 3.