使用 rspec 测试文件上传 - Rails
我想在 Rails 中测试文件上传,但不知道如何执行此操作。
这是控制器代码:
def uploadLicense
#Create the license object
@license = License.create(params[:license])
#Get Session ID
sessid = session[:session_id]
puts "\n\nSession_id:\n#{sessid}\n"
#Generate a random string
chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
newpass = ""
1.upto(5) { |i| newpass << chars[rand(chars.size-1)] }
#Get the original file name
upload=params[:upload]
name = upload['datafile'].original_filename
@license.format = File.extname(name)
#calculate license ID and location
@license.location = './public/licenses/' + sessid + newpass + name
#Save the license file
#Fileupload.save(params[:upload], @license.location)
File.open(@license.location, "wb") { |f| f.write(upload['datafile'].read) }
#Set license ID
@license.license_id = sessid + newpass
#Save the license
@license.save
redirect_to :action => 'show', :id => @license.id
end
我已经尝试过这个规范,但它不起作用:
it "can upload a license and download a license" do
file = File.new(Rails.root + 'app/controllers/lic.xml')
license = HashWithIndifferentAccess.new
license[:datafile] = file
info = {:id => 4}
post :uploadLicense, {:license => info, :upload => license}
end
How can I Analog the file upload, using rspec?
I want to test a file upload in rails, but am not sure how to do this.
Here is the controller code:
def uploadLicense
#Create the license object
@license = License.create(params[:license])
#Get Session ID
sessid = session[:session_id]
puts "\n\nSession_id:\n#{sessid}\n"
#Generate a random string
chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
newpass = ""
1.upto(5) { |i| newpass << chars[rand(chars.size-1)] }
#Get the original file name
upload=params[:upload]
name = upload['datafile'].original_filename
@license.format = File.extname(name)
#calculate license ID and location
@license.location = './public/licenses/' + sessid + newpass + name
#Save the license file
#Fileupload.save(params[:upload], @license.location)
File.open(@license.location, "wb") { |f| f.write(upload['datafile'].read) }
#Set license ID
@license.license_id = sessid + newpass
#Save the license
@license.save
redirect_to :action => 'show', :id => @license.id
end
I have tried this spec, but it doesnt work:
it "can upload a license and download a license" do
file = File.new(Rails.root + 'app/controllers/lic.xml')
license = HashWithIndifferentAccess.new
license[:datafile] = file
info = {:id => 4}
post :uploadLicense, {:license => info, :upload => license}
end
How can I simulate the file upload, using rspec?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用 fixture_file_upload 方法来测试文件上传:
将您的测试文件放在 "{Rails.root}/spec/fixtures/files" 目录中,
以防您期望该文件采用 params['upload']['datafile' 形式]
You can use fixture_file_upload method to test file uploading:
Put your test file in "{Rails.root}/spec/fixtures/files" directory
In case you were expecting the file in the form of params['upload']['datafile']
我不确定您是否可以单独使用 RSpec 测试文件上传。您尝试过水豚吗?
使用请求规范中的 capybara 的
attach_file
方法可以轻松测试文件上传。例如(此代码仅是演示):
I am not sure if you can test file uploads using RSpec alone. Have you tried Capybara?
It's easy to test file uploads using capybara's
attach_file
method from a request spec.For example (this code is a demo only):
如果您包含 Rack::Test*,只需包含测试方法
,然后您就可以使用 UploadedFile 方法:
*注意:我的示例基于 Sinatra,它扩展了 Rack,但应该与 Rails 一起使用,Rails 也使用 Rack、TTBOMK
if you include Rack::Test*, simply include the test methods
then you can use the UploadedFile method:
*NOTE: My example is based on Sinatra, which extends Rack, but should work with Rails, which also uses Rack, TTBOMK
我还没有使用 RSpec 完成此操作,但我确实有一个 Test::Unit 测试,可以执行类似的上传照片的操作。我将上传的文件设置为 ActionDispatch::Http::UploadedFile 的实例,如下所示:
类似的东西也可能适合您。
I haven't done this using RSpec, but I do have a Test::Unit test that does something similar for uploading a photo. I set up the uploaded file as an instance of ActionDispatch::Http::UploadedFile, as follows:
Something similar might work for you also.
这就是我使用
Rails 6
、RSpec
和Rack::Test::UploadedFile
不要包含
ActionDispatch::测试流程
或任何其他代码,除非您确定所包含的内容。This is how I did it with
Rails 6
,RSpec
andRack::Test::UploadedFile
DO NOT include
ActionDispatch::TestProcess
or any other code unless you're sure about what you're including.我必须添加这两个包含才能使其正常工作:
I had to add both of these includes to get it working: