11

我正在尝试使用 attachment_fu 为工作文件上传功能编写规范。但是,作者提供的用于测试的示例代码需要我要求action_controller/test_process才能访问ActionController::UploadedStringIO类。我以前在 rails 2.x 中使用过这个,但对于 rails 3,它无法找到test_process文件。

如何继续测试 rails 3 中的文件上传功能?

4

3 回答 3

23

我能够在 Rails3 和 rspec 中很好地使用 fixture_file_upload。

我刚刚添加include ActionDispatch::TestProcess到 spec_helper.rb,然后将类似以下内容作为文件数据传递:

fixture_file_upload(Rails.root.join("spec/support/test.png"), 'image/png')

编辑:我升级的东西改变了它的行为以始终包含灯具路径,所以我切换到这个而不是fixture_file_upload:

Rack::Test::UploadedFile.new(Rails.root.join("spec/support/test.png"), 'image/png')
于 2011-05-04T18:24:35.620 回答
4

我现在正在测试 Rails 3 中的文件上传,尽管我不知道我的答案会有多大帮助。你可以改变测试吗?就个人而言,我使用的是 Cucumber/Capybara 方法。Capybara 的 DSL 定义

attach_file('Image', '/path/to/image.jpg')

就个人而言,这似乎是一种比与 StringIO 对象交互更好的测试方法……请随意反驳。

于 2010-10-26T17:30:46.333 回答
3

这对于 Rails 3 来说有点棘手

您仍然可以使用该fixture_file_upload功能,但您必须添加:

include ActionDispatch::TestProcess

到你的测试用例。

这对 Rspec 根本不起作用。我只需要抓取文件本身并直接发布文件。

于 2011-04-11T20:27:01.750 回答