ruby on rails - fixture_file_upload не работает с тестами контроллера для CarrierWave - PullRequest
0 голосов
/ 25 сентября 2018

У меня проблема с тестами интеграции моего контроллера в моем проекте ROR 5.2 с использованием CarrierWave и Minitest.Я использую fixture_file_upload для «загрузки» файла, который работает корректно в моих тестах модели, но не проходит тесты интеграции моего контроллера из-за проверки наличия свойства CarrierWave в моей модели.Это всегда завершается ошибкой при создании действияДействие обновления иногда и случайно завершается неудачей, хотя я не обновлял свойство CarrierWave.

Я использовал byebug, чтобы проверить значение свойства в действии create, и он ничего не возвращает;свойство никогда не устанавливается.Я также проверил ошибки только что созданной модели: «Значок не может быть пустым».

fixture_file_upload отлично работает в моих модельных тестах и ​​выполняет создание / обновление / загрузку вручную (не в тестах) тоже отлично работает.

Я уже много часов гуглю, пытаясь выяснить, что я делаю неправильно, но все, что я нахожу, говорит об использовании fixture_file_upload, и я не нашел ничего связанного с проблемой, которую яУ меня есть.

Инициализатор CarrierWave:

CarrierWave.configure do |config|
    #To let CarrierWave work on heroku
    config.root = Rails.root.join('tmp')
    config.cache_dir = 'uploads/tmp'

    if Rails.env.test? || Rails.env.development?
        config.storage = :file

        #config for tests is done in test/test_helper.rb
    else
        config.storage = :fog

        config.fog_credentials = { #Configuration for Amazon S3
            provider: 'AWS',
            aws_access_key_id: Rails.application.credentials.aws[:access_key_id],
            aws_secret_access_key: Rails.application.credentials.aws[:secret_access_key],
            region: Rails.application.credentials.aws[:region]
        }

        config.fog_public = false
        config.fog_directory = Rails.application.credentials.aws[:bucket_name]
        config.fog_host = "#{Rails.application.credentials.aws[:asset_url]}/#{Rails.application.credentials.aws[:bucket_name]}"
    end
end

Помощник по тестированию:

ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
require 'rails/test_help'
include ActionDispatch::TestProcess #for fixture_file_upload

module UsersHelper
    def login_as(user)
        get login_user_url
        assert_response :success

        post user_login_url(session: { username: user.username, password: 'test1234' }) #have to hard code password here since passwords are stored encrypted
        assert_redirected_to root_url, 'Login did not redirect'
    end

    def logout
        get user_logout
    end
end

class ActiveSupport::TestCase
    # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
    fixtures :all
    # Add more helper methods to be used by all tests here...
end

class ActionDispatch::IntegrationTest
    include UsersHelper
end

#setup carrierwave for tests
carrierwave_root = Rails.root.join('tmp', 'test', 'support', 'carrierwave')
carrierwave_template = Rails.root.join('test', 'fixtures', 'files')

CarrierWave.configure do |config|
    config.root = carrierwave_root
    config.cache_dir = carrierwave_root.join('carrierwave_cache')
    config.enable_processing = false
end

#copy carrierwave fixture files to carrierwave root
puts 'Copying CarrierWave fixture files..'
puts carrierwave_template.join('uploads')
puts carrierwave_root
FileUtils.cp_r carrierwave_template.join('uploads'), carrierwave_root

Minitest.after_run do
    #remove carrierwave files
    puts 'Deleting CarrerWave fixture files...'

    Dir.glob(Pathname.new(carrierwave_root).join('*')).each do |dir|
        FileUtils.remove_entry(dir)
    end

    puts 'Cleaning CarrierWave cached files...'
    CarrierWave.clean_cached_files!(0)
end

Модель:

class Category < ApplicationRecord
    mount_uploader :icon, IconUploader, dependent: :destroy

    validates :name, length: { minimum: 2, maximum: 30 }, uniqueness: { case_sensitive: false }
    validates :slug, length: { minimum: 2, maximum: 30 }, uniqueness: { case_sensitive: false }
    validates :icon, presence: true
end

IconUploader:

class IconUploader < CarrierWave::Uploader::Base
    include CarrierWave::MiniMagick

    after :remove, :delete_empty_upstream_dirs

    def store_dir
        "#{base_store_dir}/#{model.id}"
    end

    def base_store_dir
        "uploads/#{model.class.to_s.underscore}/#{mounted_as}"
    end

    #override file name, for uniqueness
    def filename
        random_token = SecureRandom.hex(6/2) #length of 6 characters
        token_var = "@#{mounted_as}_secure_token" #get token variable name
        token = model.instance_variable_get(token_var) #get token from token variable name
        token ||= model.instance_variable_set(token_var, random_token) #if token isn't already set, set it

        @name ||= "#{token}_#{super}" if original_filename.present? and super.present? #create name, using instance variable so token isn't changed (function is called multiple times)
    end

    #set size limits
    def size_range
        1.kilobyte..256.kilobytes #1 kilobyte to 256 kilobytes
    end

    #resize image if width or height is greater than 256px, add padding if needed
    process resize_and_pad: [256, 256] #don't use resize_to_fit, as it adds a white background at least to SVG images

    # Add a white list of extensions which are allowed to be uploaded.
    # For images you might use something like this:
    def extension_whitelist
        %w(jpg jpeg png svg)
    end

    #whitelist of content types
    def content_type_whitelist
        /image\// #whitelist images
    end

    private

    #delete directory if it's empty
    def delete_empty_upstream_dirs
        path = ::File.expand_path(store_dir, root)
        Dir.delete(path) #fails if path not empty dir

        path = ::File.expand_path(base_store_dir, root)
        Dir.delete(path) #fails if path not empty dir
    rescue SystemCallError => e
        Rails.logger.error(e.message) #log error

        true #nothing, the dir is not empty
    end
end

Действие создания контроллера:

def create
    data = params.require(:category).permit([ :name, :icon, :icon_cache ])

    @category = Category.new(data)

    if @category.save
        flash.notice = 'Category successfully created.'
        redirect_to categories_path
    else
        render :add #show errors
    end
end

Тест контроллера:

test "should post category_create when admin" do
    login_as(users(:admin))

    get add_category_url
    assert_response :success

    icon = fixture_file_upload(Rails.root.join('test', 'fixtures', 'files', 'category_icon.svg'))

    #fails: validation error: "Icon can't be blank"
    post category_create_url(category: { name: 'test901', icon: icon, icon_cache: '' })
    assert_redirected_to categories_url
    assert_equal 'Category successfully created.', flash[:notice]
end

Тест модели:

test "should save when all details correct" do
    category = Category.new(name: 'tools',
            icon: fixture_file_upload(Rails.root.join('test', 'fixtures', 'files', 'category_icon.svg')))

    #succeeds
    assert category.save, 'Not saved when all details correct: ' + category.errors.full_messages.to_s
end

1 Ответ

0 голосов
/ 09 января 2019
post category_create_url(category: { name: 'test901', icon: icon, icon_cache: '' })

должно быть

post category_create_url, params: {category: { name: 'test901', icon: icon, icon_cache: '' }}

Первая отправляет параметры в route_helper и приводит к попытке передать файл через параметры строки запроса, которые не будут работать.

Вторая отправляет параметры в метод post, который правильно передает параметры в виде данных multipart / form, которые правильно отправляют объект файла в контроллер.

...