Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何测试文件上载和控制器?_Ruby On Rails_File Upload_Rspec - Fatal编程技术网

Ruby on rails 如何测试文件上载和控制器?

Ruby on rails 如何测试文件上载和控制器?,ruby-on-rails,file-upload,rspec,Ruby On Rails,File Upload,Rspec,我的看法 我的Rspec测试(不起作用) 错误:失败/错误:post:upload\u image,:upload=>@file,:car\u id=>@car.id 命名错误: #文件的未定义方法“[]”:/tmp/test-bus-1.jpg20120826-29027-plg28d 怎么了?根据您设置表单的方式(因为您正在调用params[:upload][:datafile]),我认为您需要将rspec测试更改为: before :each do @car = FactoryGirl

我的看法

我的Rspec测试(不起作用)

错误:失败/错误:post:upload\u image,:upload=>@file,:car\u id=>@car.id 命名错误: #文件的未定义方法“[]”:/tmp/test-bus-1.jpg20120826-29027-plg28d


怎么了?

根据您设置表单的方式(因为您正在调用
params[:upload][:datafile]
),我认为您需要将rspec测试更改为:

before :each do
  @car = FactoryGirl.create(:car)
  @file = fixture_file_upload('/files/test-bus-1.jpg', 'image/jpg')

end

it "can upload a car" do
  post :upload_image, :upload => @file, :car_id => @car.id
  response.should redirect_to images_path(:car_id => @car.id)
end

我想,params[:upload][:datafile]中的问题。。。
  def upload_image   
    if params[:upload].present? && params[:car_id].present?
      DataFile.save_image_file(params[:upload][:datafile], params[:car_id]) # My methods to save image and other operations with file

    end
    redirect_to images_path(:car_id => params[:car_id])
  end
before :each do
  @car = FactoryGirl.create(:car)
  @file = fixture_file_upload('/files/test-bus-1.jpg', 'image/jpg')

end

it "can upload a car" do
  post :upload_image, :upload => @file, :car_id => @car.id
  response.should redirect_to images_path(:car_id => @car.id)
end
post :upload_image, :upload => { :datafile => @file }, :car_id => @car.id