Rails 功能测试用例和上传文件到 ActionDispatch::Http::UploadFile

发布于 2024-10-08 19:34:54 字数 574 浏览 1 评论 0原文

我正在向远程存储文件的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不必你懂 2024-10-15 19:34:54

我使用 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 文件对象并使用它:

upload = ActionDispatch::Http::UploadedFile.new({
  :filename => 'avatar.jpeg',
  :type => 'image/jpeg',
  :tempfile => File.new("#{Rails.root}/test/fixtures/avatar.jpeg")
})

post :create, :person => { :avatar => upload }

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:

upload = ActionDispatch::Http::UploadedFile.new({
  :filename => 'avatar.jpeg',
  :type => 'image/jpeg',
  :tempfile => File.new("#{Rails.root}/test/fixtures/avatar.jpeg")
})

post :create, :person => { :avatar => upload }
紫﹏色ふ单纯 2024-10-15 19:34:54

我建议使用模拟。
快速谷歌搜索显示:

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.

池予 2024-10-15 19:34:54

我不熟悉 AWS/S3 gem,但看来您可能没有正确使用 :avatar 参数。 bytesize 是在 ruby​​1.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 on String in ruby1.9. What happens if you call read on the uploaded file where you pass it into AWS/S3?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文