Rails 功能测试用例和上传文件到 ActionDispatch::Http::UploadFile
我正在向远程存储文件的 Rails 应用程序添加测试。我正在使用默认的 Rails 功能测试。如何向其中添加文件上传?我有:
test "create valid person" do
post(:create, :person => { :avatar => fixture_file_upload('avatar.jpeg') })
end
由于某种原因,这会上传 Tempfile
并导致 AWS/S3
gem 失败:
NoMethodError: undefined method `bytesize' for Tempfile
他们有什么方法可以让我进行测试以使用 >ActionDispatch::Http::UploadedFile
并执行更像使用 Web 浏览器测试时的操作? fixture_file_upload
是测试将文件上传到控制器的方法吗?如果是这样,为什么它不像浏览器那样工作?
需要注意的是,我真的不想切换测试框架。谢谢!
I'm am adding tests to a Rails app that remotely stores files. I'm using the default Rails functional tests. How can I add file uploads to them? I have:
test "create valid person" do
post(:create, :person => { :avatar => fixture_file_upload('avatar.jpeg') })
end
This for some reason uploads a Tempfile
and causes the AWS/S3
gem to fail with:
NoMethodError: undefined method `bytesize' for Tempfile
Is their any way that I can get the test to use an ActionDispatch::Http::UploadedFile
and perform more like it does when testing with the web browser? Is fixture_file_upload
the way to test uploading files to a controller? If so why doesn't it work like the browser?
As a note, I really don't want to switch testing frameworks. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我使用 s3 gem 而不是 aws/s3 gem。造成这种情况的主要原因是不支持欧洲存储桶,并且aws/s3的开发似乎已停止。
如果您想测试文件上传,那么使用fixtures_file_upload方法是正确的,它直接映射到Rack::Test::UploadedFile.new(如果测试成功,您可以使用此方法)文件不在装置文件夹中)。
但我也注意到 Rack::Test::Uploaded 文件对象的行为与 ActionDispatch::Http::UploadedFile 对象(即上传文件的类)并不完全相同。基本方法(original_filename、read、size...)都可以工作,但在使用 file 方法时存在一些差异。因此,将您的控制器限制为这些方法,一切都会好起来的。
另一种可能的解决方案是创建一个 ActionDispatch::Http::Uploaded 文件对象并使用它:
I use the s3 gem instead of the aws/s3 gem. The main reasons for this are no support for european buckets and development of aws/s3 seems to be stopped.
If you want to test file upload than using the fixtures_file_upload method is correct, it maps directly to Rack::Test::UploadedFile.new (you can use this if the test file isn't in the fixtures folder).
But I've also noticed that the behavior of the Rack::Test::Uploaded file objects isn't exactly the same as the ActionDispatch::Http::UploadedFile object (that's the class of uploaded files). The basic methods (original_filename, read, size, ...) all work but there are some differences when working with the file method. So limit your controller to these methods and all will be fine.
An other possible solution is by creating an ActionDispatch::Http::Uploaded file object and using that so:
我建议使用模拟。
快速谷歌搜索显示:
http://www.ibm.com /developerworks/web/library/wa-mockrails/index.html
您应该能够创建一个对象来响应您想要的行为。主要用于单元测试环境,因此您可以单独测试您的内容,因为集成测试应该充分运用整个堆栈。不过,我认为在这种情况下模拟 S3 服务会很有用,因为它需要花钱。
I'd recommend using mocks.
A quick google search reveals:
http://www.ibm.com/developerworks/web/library/wa-mockrails/index.html
You should be able to create an object that will respond to the behaviors you want it to. Mostly used in a Unit test environment, so you can test your stuff in isolation, as integration tests are supposed to fully exercise the entire stack. However, I can see in this case it'd be useful to mock out the S3 service because it costs money.
我不熟悉 AWS/S3 gem,但看来您可能没有正确使用 :avatar 参数。
bytesize
是在 ruby1.9 中的String
上定义的。如果您对上传的文件调用read
并将其传递到 AWS/S3,会发生什么?I'm not familiar with the AWS/S3 gem, but it seems that you probably aren't using the :avatar param properly.
bytesize
is defined onString
in ruby1.9. What happens if you callread
on the uploaded file where you pass it into AWS/S3?