我从您的项目中阅读了代码,发现您提出了一个有趣的解决方案,但这里有另一个符合您要求的解决方案。
第 1 步 - 添加摩卡咖啡
Mocha 是 MiniTest 粉丝事实上的存根库,所以像这样将它添加到您的 Gemfile 中。
group :test do
gem 'mocha'
end
然后运行bundle install 并在您的测试助手中要求 mocha,如下所示:
# test/test_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
require 'rails/test_help'
# add this line
require 'mocha/minitest'
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
end
第 2 步 - 创建一个虚拟测试文件
在test/fixtures/files/ 中放置一个名为“test.png”的小文件。请注意,我的文件大小约为 20KB,非常小。
第 3 步 - 添加单元测试
将以下内容添加到test/models/page_test.rb:
test 'file has max size' do
# get the path of the file
fixture_path = File.join(Rails.root, 'test', 'fixtures', 'files', 'test.png')
# open the file
fixture_file = File.open(fixture_path)
# stub the file size to 21 MB
fixture_file.stubs(:size).returns(21.megabytes)
# attach the file to your @page instance
@page.file.attach(io: fixture_file, filename: 'test.png')
# the record should now be invalid
refute @page.valid?
# it should not have an attachment
refute @page.file.attached?
# assert the error message matches a regex
assert_match /is too big/, @page.errors[:file].to_s
end
第 4 步 - 在最大文件大小边界内测试
让我们通过将以下内容添加到test/models/page_test.rb 来确保可以接受 20MB 的文件:
test 'file with valid size' do
# get the path of the file
fixture_path = File.join(Rails.root, 'test', 'fixtures', 'files', 'test.png')
# open the file
fixture_file = File.open(fixture_path)
# stub the file size to 20 MB
fixture_file.stubs(:size).returns(20.megabytes)
# attach the file to your @page instance
@page.file.attach(io: fixture_file, filename: 'test.png')
# the record should now be valid
assert @page.valid?
# it should have an attachment
assert @page.file.attached?
# probably unnecessary, but let's test that there are no errors on @page
assert_empty @page.errors[:file]
end
第 5 步 - 为快乐路径添加控制器测试
让我们为快乐路径添加一个控制器测试:
test 'should update page' do
# fixture_file_upload knows where the fixtures folder is
# and makes sure we have a correctly signed file
fixture_file = fixture_file_upload('files/test.png', 'image/png')
# we need to stub the byte_size of any instance of ActiveStorage::Blob
# as that is what our validations are going to be run against
ActiveStorage::Blob.any_instance.stubs(:byte_size).returns(20.megabytes)
# make sure our ActiveStorage records are created
assert_difference ['ActiveStorage::Blob.count', 'ActiveStorage::Attachment.count'] do
# add our fixture file into the params
patch page_url, params: { page: { file: fixture_file } }
end
# make sure we are redirected to the right place
assert_redirected_to page_url
end
第 6 步 - 为悲伤路径添加控制器测试
测试上传失败的时间:
test 'should render edit template when update fails' do
# like before, we get correctly formatted test file by calling
# fixture_file_upload method and passing in the test file
fixture_file = fixture_file_upload('files/test.png', 'image/png')
# stub the byte_size to be 21MB
ActiveStorage::Blob.any_instance.stubs(:byte_size).returns(21.megabytes)
# make sure zero ActiveStorage records are created
assert_no_difference ['ActiveStorage::Blob.count', 'ActiveStorage::Attachment.count'] do
# send the request
patch page_url, params: { page: { file: fixture_file } }
end
# check the response body for an error message
assert_match /is too big/, response.body.to_s
end
就是这样。希望对您有所帮助。