2015-05-13 56 views
0

我有一個類SeatingChart在工廠的SeatingChart我試圖創建chart_imageRack::Test::UploadedFile附加chart_image並正在此錯誤:FactoryGirl用回形針設置了錯誤的圖像內容類型

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 

回答

0

指定文件類型後,新的錯誤信息,您可以指定像這樣的內容類型:

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

這設置圖像類型,它應該是什麼,但我仍然收到錯誤消息。我向OP添加了新的錯誤消息 – ThomYorkkke