简体   繁体   English

测试文件是否在Rails中成功上传

[英]Testing if a file uploads sucessfully in Rails

I'm running Rails 3.2.13. 我正在运行Rails 3.2.13。

In one of my controllers, the application processes a form by which the users sends a file. 在我的一个控制器中,应用程序处理用户发送文件的表单。 The application analyses the file and saves it OR NOT, depending on the analysis. 应用程序将分析文件并保存或不保存文件,具体取决于分析。

I want to write a functional test which tests that the correct file gets saved, and that the wrong file doesn't get saved. 我想编写一个功能测试,测试是否保存了正确的文件,并且没有保存错误的文件。

I'm loading the file from test/fixtures/files, like this: 我正在从test / fixtures / files加载文件,如下所示:

resource_file = Rack::Test::UploadedFile.new(Rails.root.
    join('test', 'fixtures', 'files', 'f1.jpg'))

When running the test, where does this file gets saved? 运行测试时,该文件保存在哪里?

/spec/support/fixture_file.rb /spec/support/fixture_file.rb

include ActionDispatch::TestProcess
def fixture_file(filename, extension)
  fixture_file_upload(Rails.root.join('spec', 'fixtures', filename), extension)
end

/spec/factories/offers.rb /spec/factories/offers.rb

FactoryGirl.define do 
  factory :offer do
    large_picture { fixture_file("ostrov.jpg", "image/jpeg") }
  end
end

PS It also works for TestUnit PS它也适用于TestUnit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM