2012-08-25 49 views
0

我的觀點如何測試文件上傳和控制器?

<%= form_tag({:action => 'upload_image', :car_id => @car.id}, :multipart => true) do %> 
    <label for="upload_file"><%= t('field.select_image') %></label> 
    <%= file_field 'upload', 'datafile' %>&nbsp;<%= submit_tag t('field.upload_file') %> 
    <% end %> 

我控制器

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 

我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 

錯誤:故障/錯誤:職務:upload_image,:上傳=> @file,:car_id => @ car.id NoMethodError: 未定義的方法`[]'爲#文件:/tmp/test-bus-1.jpg20120826-29027-plg28d

怎麼了?

+0

我認爲,在PARAMS問題[ :上傳] [:數據文件] ... – user1466717

回答

1

在我看來,根據您如何設置您的形式(因爲你在呼喚params[:upload][:datafile]),你需要你的rspec的測試更改爲:

post :upload_image, :upload => { :datafile => @file }, :car_id => @car.id