Ruby on rails 如何测试文件上载和控制器?
我的看法 我的Rspec测试(不起作用) 错误:失败/错误:post:upload\u image,:upload=>@file,:car\u id=>@car.id 命名错误: #文件的未定义方法“[]”:/tmp/test-bus-1.jpg20120826-29027-plg28dRuby 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
怎么了?根据您设置表单的方式(因为您正在调用
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