【问题标题】:Is there any way to test file upload in Rspec using a public URL?有没有办法使用公共 URL 在 Rspec 中测试文件上传?
【发布时间】:2013-12-24 05:53:23
【问题描述】:

我是 Spec 的新手,我想知道是否可以在我们的公共文件夹中没有文件的情况下测试文件上传。上传后,我将文件保存到 S3。目前我有这个代码。

file = ActionController::TestUploadedFile.new('./spec/fixtures/files/IMG_8880.jpg')
attach["attachment#{i+1}"] = file
attachment_in["attachment#{i+1}"] = {:filename => "IMG_8880.jpg", 
                                     :name => "IMG_8880.jpg", 
                                     :type => 'image/jpeg'}

我使用的是 rails 2.3.8,因此 fixture_file_upload 不可用。我希望有一种方法可以避免将图像文件放在我的灯具文件夹中并从某个 URL 获取它。

【问题讨论】:

  • 你用什么 gem 来上传? carrierwavepaperclip?

标签: ruby-on-rails ruby testing rspec


【解决方案1】:

我觉得在这种情况下您需要使用 Mocking 和 Stubbing。

你想测试一下;

ActionController::TestUploadedFile.should_recieve(:new).with('SOME_MOCK_OBJECT')

您可以设置 SOME_MOCK_OBEJCTby

SOME_MOCK_OBJECT = double 'SOME_MOCK_OBJECT'

您需要研究 Stubbing 和 Mocking,这是一个很好的 video,或者您可以尝试 Code Schools Rspec。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多