我从这篇优秀文章中学习了如何测试图像文件上传: https://jeffkreeftmeijer.com/2010/2014/using-test-fixtures-with-carrierwave/
为我的应用调整这些想法,我已经得到了有效的测试:
test "uploads an image" do
pic = Picture.create(:image => fixture_file_upload('/files/DJ.jpg','image/jpg'), :user => User.first)
assert(File.exists?(pic.reload.image.file.path))
end
我想为用户界面测试相同的东西,所以我认为这个测试也会通过:
test "create new picture" do
capybara_login(@teacher_1)
click_on("Upload Pictures")
fill_in "picture_name", with: "Apple"
attach_file('picture[image]', Rails.root + 'app/assets/images/apple.png')
check("check_#{@user_l.id}")
check("check_#{@admin_l.id}")
click_on ("Create Picture")
@new_pic = Picture.last
assert_equal "Apple", @new_pic.name
assert @new_pic.labels.include?(@user_l)
assert @new_pic.labels.include?(@admin_l)
assert @teacher_1, @new_pic.user
assert File.exists?(@new_pic.reload.image.file.path)
end
但它在最后一行失败,断言文件的存在。
以下是我认为与我相关的test_helper部分。如果有人认为有必要,我可以展示整个test_helper。
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require "minitest/reporters"
require 'capybara/rails'
Minitest::Reporters.use!
CarrierWave.root = 'test/fixtures/files'
class CarrierWave::Mount::Mounter
def store!
# Not storing uploads in the tests
end
end
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
include ApplicationHelper
include ActionDispatch::TestProcess
CarrierWave.root = Rails.root.join('test/fixtures/files')
def after_teardown
super
CarrierWave.clean_cached_files!(0)
end
end
在开发和生产过程中,一切似乎都按预期工作。
提前感谢您的任何见解。