我一直在研究 Carrierwave。通过将 Carrierwave 的主分支添加到我的 Gemfile,我终于能够让载波上传多个文件:
gem 'carrierwave', github:'carrierwaveuploader/carrierwave'
我将它用于一个名为 Product 的模型,然后我就这样做了。 产品.rb
class Product < ActiveRecord::Base
mount_uploaders :avatar, ProductUploader
end
我基本上遵循了规则并且能够上传多个文件,同时让每个文件通过我的 products_controller.rb
中的创建方法进行迭代,并为每个上传的文件创建一个新的 Product 实例。
现在。测试来了。以前,当只上传一个文件时,我可以使用,
test "should create product" do
login
excel_filename = 'files/product_create_test.xls'
file = fixture_file_upload(excel_filename, 'application/excel')
assert_difference('Product.count') do
post :create, product: {:file_url => file}
end
但是现在,添加了上传多个文件的功能后,fixture_file_upload 似乎无法正常工作。 我收到此错误:
ProductsControllerTest#test_should_create_product:
ArgumentError: wrong number of arguments (2 for 0)
test/controllers/products_controller_test.rb:54:in `block (2 levels) in <class:ProductsControllerTest>'
test/controllers/products_controller_test.rb:53:in `block in <class:ProductsControllerTest>'
我不确定如何解决这个问题。就像我说的,当我有
gem 'carrierwave'
之前的测试工作正常。有没有人遇到过这种情况?
最佳答案
这就是问题所在。为了使我的夹具文件上传实际上没有保存在我的测试环境中,我遵循了这篇文章,http://jeffkreeftmeijer.com/2014/using-test-fixtures-with-carrierwave/#comment_768 .
这里它告诉您将以下内容添加到您的 test_helper 文件
class CarrierWave::Mount::Mounter
def store!
# Not storing uploads in the tests
end
end
这实质上告诉您的测试环境不要将上传的固定装置存储在任何地方。无论如何,这段代码在添加用于多个文件上传的载波主分支时运行不佳。我最终取消了它的注释,尽管它存储了上传的文件,但我的测试通过了。
关于ruby-on-rails - CarrierWave 多文件上传 Rails 单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32490966/