如何使用Factory Girl生成回形针附件?

发布于 2024-09-10 13:10:49 字数 934 浏览 5 评论 0原文

我有一个包含许多图像的模型 Person,其中图像有一个称为数据的回形针附件字段,下面显示一个缩写版本:

class Person
  has_many :images
  ...
end

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

Person 需要至少附加一个图像。

使用 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。

I have model Person that has many Images, where images has a Paperclip attachment field called data, an abbreviated version displayed below:

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:

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)

...

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?

I am using Rails 3.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

冷心人i 2024-09-17 13:10:49

使用 fixture_file_upload

您可以在测试中 include ActionDispatch::TestProcess helper,这里是一个示例工厂:

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 需要存在于应用程序的根目录中。

请注意,FactoryBotFactoryGirl 的新名称。

You can use fixture_file_upload

include ActionDispatch::TestProcess in your test helper, here is an example factory:

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.png needs to exist in your application's root directory before running your tests.

Note, that FactoryBot is a new name for FactoryGirl.

绝影如岚 2024-09-17 13:10: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

Newer FG syntax and no includes necessary

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
平安喜乐 2024-09-17 13:10:49

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

Desmond Bowe over at Pivotal Labs suggests avoiding fixture_file_upload due to memory leak problems. Instead, you should set the paperclip fields directly in your factory:

factory :attachment do
  supporting_documentation_file_name { 'test.pdf' }
  supporting_documentation_content_type { 'application/pdf' }
  supporting_documentation_file_size { 1024 }
  # ...
end
自找没趣 2024-09-17 13:10:49

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

Rails 2

http://gist.github.com/162881

Rails 3

https://gist.github.com/313121

I've been using the code in the gist below:

Rails 2

http://gist.github.com/162881

Rails 3

https://gist.github.com/313121

旧伤还要旧人安 2024-09-17 13:10:49
file { File.new(Rails.root.join('spec', 'fixtures', 'filename.png')) }
file { File.new(Rails.root.join('spec', 'fixtures', 'filename.png')) }
倦话 2024-09-17 13:10:49

尝试使用 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'))
   ] 
}

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'))
   ] 
}
吃→可爱长大的 2024-09-17 13:10:49

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

第一种方法:

对我来说,添加 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
}

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
}
自由如风 2024-09-17 13:10:49

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

您尝试过吗?

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

我们使用 Machinist 而不是factory_girl,并且刚刚使用了类似的东西。

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

尽管如此,我们在执行此操作时并没有真正进行太多测试,但我们通常只想拥有一个有效的 Image 对象。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文