简体   繁体   English

CarrierWave多文件上传Rails单元测试

[英]CarrierWave Multiple File Upload Rails Unit Testing

I have been banging my head with Carrierwave. 我一直在用Carrierwave敲头。 I finally was able to get carrier wave to upload multiple files by adding the master branch for Carrierwave to my Gemfile: 通过将Carrierwave的主分支添加到我的Gemfile中,我终于能够使载波上传多个文件:

gem 'carrierwave', github:'carrierwaveuploader/carrierwave'

I used it for a model called Product and I did this. 我将其用于名为Product的模型中,并执行了此操作。 Product.rb Product.rb

class Product < ActiveRecord::Base
  mount_uploaders :avatar, ProductUploader
end

And I essentially followed the rules and was able to upload multiple files all while having each file iterate through the create method in my products_controller.rb and creating a new Product instance for each file being uploaded. 而且我基本上遵循规则,并且能够在我的products_controller.rb通过create方法迭代每个文件并为每个要上传的文件创建一个新的Product实例的同时上传所有文件。

Now. 现在。 Here comes the Testing. 测试来了。 Before, when only one file was being uploaded I was able to use, 以前,当我只能上传一个文件时,

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

But now, after adding the ability to upload multiple files, it seems like fixture_file_upload is not working properly. 但是现在,添加了上传多个文件的功能后,fixture_file_upload似乎无法正常工作。 I am getting this error: 我收到此错误:

 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>'

I am not sure how to go around this. 我不确定该如何解决。 Like I said, when I had 就像我说的,当我有

 gem 'carrierwave' 

the previous test worked fine. 以前的测试效果很好。 Has anyone ever encountered this? 有人遇到过吗?

Here's what the problem was. 这就是问题所在。 In order to make it so that my fixture file uploads did not actually save in my test enviroment, I followed this article, http://jeffkreeftmeijer.com/2014/using-test-fixtures-with-carrierwave/#comment_768 . 为了使我的夹具文件上传实际上没有保存在我的测试环境中,我遵循了这篇文章, http://jeffkreeftmeijer.com/2014/using-test-fixtures-with-carrierwave/#comment_768

Here it tells you to add the following to your test_helper file 在这里,它告诉您将以下内容添加到您的test_helper文件中

class CarrierWave::Mount::Mounter
  def store!
    # Not storing uploads in the tests
  end
end

This essentially tells your testing environment not to store the uploaded fixtures anywhere. 这实际上告诉您的测试环境不要在任何地方存储上载的灯具。 Anyway, this piece of code does not fly well with the addition of the carrier wave master branch for multiple file uploads. 无论如何,如果为多个文件上传添加了载波主分支,那么这段代码将无法很好地运行。 I ended up uncommenting it and despite the fact that is stores the uploaded files, my tests pass. 我最终取消了对它的注释,尽管事实是它存储了已上传的文件,但我的测试还是通过了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM