FactoryGirl与Paperclip设置错误的图像内容类型

时间:2015-05-13 23:34:31

标签: ruby-on-rails rspec paperclip factory-bot

我在SeatingChart工厂中有一个附加chart_image的课程SeatingChart我正在尝试使用chart_image创建Rack::Test::UploadedFile并且正在获取这个错误:

Failures:

  1) SeatingChart has a minimum valid factory
     Failure/Error: expect(build :seating_chart).to be_valid
       expected #<SeatingChart id: nil, name: "Konklab", created_at: nil, updated_at: nil, venue_id: nil, chart_image_file_name: "logoimage.jpg", chart_image_content_type: "text/plain", chart_image_file_size: 28555, chart_image_updated_at: "2015-05-13 23:29:01"> to be valid, but got errors: Chart image content type is invalid, Chart image is invalid

SeatingChart的工厂是

FactoryGirl.define do
  factory :seating_chart do
    name { Faker::App.name }
    chart_image do
      Rack::Test::UploadedFile.new Rails.root.join('app',
                                          'assets',
                                          'images',
                                          'logoimage.jpg')
    end
    factory :chart_with_seats do
      after :build do |chart|
        50.times do
          create :seat, seating_chart: chart
        end
      end
    end
  end
end

SeatingChart

class SeatingChart < ActiveRecord::Base
  require 'json'
  has_attached_file :chart_image
  validates_attachment_content_type :chart_image, content_type: %r{ /\Aimage\/.*\Z/ }
  validates_attachment :chart_image, presence: true
  validates :name, presence: true

  has_many :shows
  has_many :price_sections
end

编辑:在Rack::Test::UploadedFile.new

中指定文件类型后出现新的错误消息
Failures:

  1) SeatingChart has a minimum valid factory
     Failure/Error: expect(build :seating_chart).to be_valid
       expected #<SeatingChart id: nil, name: "Span", created_at: nil, updated_at: nil, venue_id: nil, chart_image_file_name: "logoimage.jpg", chart_image_content_type: "image", chart_image_file_size: 28555, chart_image_updated_at: "2015-05-13 23:44:02"> to be valid, but got errors: Chart image content type is invalid, Chart image is invalid

1 个答案:

答案 0 :(得分:0)

您可以像这样指定内容类型:

Rack::Test::UploadedFile.new (Rails.root.join('app',
                                          'assets',
                                          'images',
                                          'logoimage.jpg'),
                              'image/jpg'
)