我在使用CarrierWave和Minitest的ROR 5.2项目上进行控制器集成测试时遇到问题。我正在使用fix_file_upload“上传”一个文件,该文件在模型测试中可以正常工作,但是由于模型中对CarrierWave属性的存在验证,在控制器集成测试中失败。创建操作总是失败。即使我没有更新CarrierWave属性,更新操作也偶尔会随机失败。
我使用byebug来检查create动作上的属性值,并且它什么也不返回;该属性从未设置。我还检查了新创建的模型的错误,它们是:“图标不能为空”。
fixture_file_upload在我的模型测试中工作正常,并且手动进行创建/更新/上传(在测试中则没有)
我已经在Google上搜索了多个小时,以弄清自己在做错什么,但是我发现的所有内容都说要使用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
答案 0 :(得分:2)
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方法,该方法将参数正确地作为多部分/表单数据发布,从而将文件对象正确发布到控制器。