Rails 3 test fixtures with carrierwave?

前端 未结 6 1107
轻奢々
轻奢々 2021-02-05 09:02

I\'m working on upgrading from attachment_fu to carrierwave, since attachment_fu is broken in rails 3.

None of the tests are able to run, because we have invalid fixture

6条回答
  •  忘掉有多难
    2021-02-05 09:44

    I know it is old but, for some that uses Rails 5 + RSpec + CarrierWave + Fixtures:

    Edit test configs:

    # config/initializers/carrierwave.rb
    if Rails.env.test?
      class NullStorage < CarrierWave::Storage::Abstract
        def store!(_file)
          _file
        end
    
        def retrieve!(identifier)
          file = Rails.root.join('spec', 'fixtures', 'files', identifier)
          tmp = Rails.root.join('tmp', 'blank_tmp.jpg')
          FileUtils.cp(file, tmp)
          CarrierWave::SanitizedFile.new(tmp)
        end
      end
    
      CarrierWave.configure do |config|
        config.storage = NullStorage
        config.enable_processing = false
      end
    end
    

    Create a folder and a file, for example spec/fixtures/files/some-user-photo.jpg

    and, create some fixtures, for example:

    first_user:
      avatar: "some-user-photo.jpg"
      name: "First User Name"
      about: "First User About Long Text..."
      lat: 0.001
      lng: 0.001
      created_at: <%= Time.current - 3.days %>
      updated_at: <%= Time.current - 3.days %>
    

    That is enough to make the test understand that this user has an avatar

提交回复
热议问题