以下是我上传文件的测试代码.
describe "file process" do
before(:each) do
# debugger
@file = fixture_file_upload('test.csv', 'text/csv')
end
it "should be able to upload file" do
post :upload_csv, :upload => @file
response.should be_success
end
end
Run Code Online (Sandbox Code Playgroud)
但是,当我运行rspec规范时,它会产生下面的错误
Failure/Error: @file = fixture_file_upload('test.csv', 'text/csv')
RuntimeError:
test.csv file does not exist
# ./spec/controllers/quotation_controller_spec.rb:29:in `block (3 levels) in <top (required)>'
Run Code Online (Sandbox Code Playgroud)
我搜索了很多地方,但我仍然无法找出背后的原因.任何的想法?
我正在尝试使用attachment_fu为工作文件上传功能编写规范.但是,作者提供的用于测试的示例代码要求我要求action_controller/test_process以便我可以访问ActionController::UploadedStringIO类.我之前在rails 2.x中使用过这个,但对于rails 3,它无法找到test_process文件.
如何继续测试rails 3中的文件上传功能?
我正在使用这段代码来模仿文件的上传方式:
def mock_file
file = File.new((Rails.root + "public/checklist_items_template.csv"),"r")
image = ActionDispatch::Http::UploadedFile.new(
:filename => "checklist_items_template.csv",
:type => "text/csv",
:head => "Content-Disposition: form-data;
name=\"checklist_items_template.csv\";
filename=\"checklist_items_template.csv\"
Content-Type: text/csv\r\n",
:tempfile => file)
return image
end
Run Code Online (Sandbox Code Playgroud)
在rspec测试中,它是POST给控制器:
post :create, :legal_register_id => "1", :register => {"file" => mock_file}
Run Code Online (Sandbox Code Playgroud)
但它在实际控制器中打破了这一行:
CSV.parse(params[:register][:file].read.force_encoding('UTF-8'))
Run Code Online (Sandbox Code Playgroud)
因为params [:register] [:file]被解释为字符串而不是actiondispatch对象:
undefined method `read' for "#<ActionDispatch::Http::UploadedFile:0x00000108de3da8>":String
Run Code Online (Sandbox Code Playgroud)
这是rspec的标准行为吗?有没有办法通过参数传递对象?