代码之家  ›  专栏  ›  技术社区  ›  Laz

如何使用工厂女孩生成回形针附件?

  •  118
  • Laz  · 技术社区  · 14 年前

    我有很多图片的模特,其中图片有一个叫做数据的回形针附件字段,下面显示的是一个缩写版本:

    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::enent)

    有时测试运行成功。

    有人能告诉我我在这里有什么问题吗?或者他们是如何利用工厂女孩和回形针来达到我想要达到的目标的?

    我在使用3号轨道。

    8 回复  |  直到 6 年前
        1
  •  84
  •   Paweł Gościcki Ywain    6 年前

    你可以用 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 .

        2
  •  47
  •   Neal    11 年前

    更新的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
    
        3
  •  37
  •   letz    8 年前

    德斯蒙德·鲍尔在关键实验室 suggests avoiding fixture_file_upload 由于内存泄漏问题。相反,您应该直接在工厂中设置曲别针字段:

    factory :attachment do
      supporting_documentation_file_name { 'test.pdf' }
      supporting_documentation_content_type { 'application/pdf' }
      supporting_documentation_file_size { 1024 }
      # ...
    end
    
        4
  •  25
  •   deb    13 年前

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

    轨道2

    http://gist.github.com/162881

    轨道3

    https://gist.github.com/313121

        5
  •  12
  •   Sebastián Palma    7 年前
    file { File.new(Rails.root.join('spec', 'fixtures', 'filename.png')) }
    
        6
  •  4
  •   britt    14 年前

    尝试使用ActionController::TestUploadedFile。只需将文件属性设置为testuploadedfile的一个实例,paperclip就可以处理其余的内容。例如

    valid_file = File.new(File.join(Rails.root, 'features', 'support', 'file.png'))  
    p.images { 
       [
         ActionController::TestUploadedFile.new(valid_file, Mime::Type.new('application/png'))
       ] 
    }
    
        7
  •  0
  •   Aleks    7 年前

    在某些情况下,上面的答案是有帮助的,而在我的某个情况下,上面的答案实际上是有帮助的,但是当使用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
    }
    
        8
  •  -1
  •   theIV    14 年前

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

    你试过了吗

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

    我们用的是机械师,而不是工厂女工,只是用过

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

    但是,我们在进行此操作时并没有进行太多的测试,我们通常只希望 Image 对象。