在规格说明中截断Paperclip S3请求

13

我正在使用Paperclip和S3进行图像上传,正在尝试在我的测试套件中对S3的调用进行存根。我找到了thoughtbot的帖子,其中提到要执行此操作:

  a.cover       { a.paperclip_fixture('album', 'cover', 'png') }

但这给我一个“参数错误(4个与2个)”。我尝试将上面的参数切换为数组,这样可以消除原始错误,但会出现“属性已定义:paperclip_fixture”的错误。

有人能够让这个工作吗?此外,我最好在开发环境中使用本地文件系统。有什么简单的方法可以做到这一点吗?


请问您能否提供更多关于您的代码的信息?例如,a.cover是什么意思?我猜测a是您的对象,cover是包含图像URI的属性? - hwrd
另外,你究竟想要测试什么?更多的上下文会有所帮助。 - hwrd
8个回答

7

好的,我已经解决了基本问题。这是因为我没有使用shoulda(而是使用rspec 2.6.0factory_girl 2.1.2),我认为这就像Eliza所说的那样。

以下是对我有效的内容(其中Profile是具有附件的类):

  Profile.any_instance.stub(:save_attached_files).and_return(true)
  @profile = Factory(:profile)

目前,我只是在我的 rspec 示例的 before 方法中添加了这个。可能有更好的地方可以放置它。


6
使用最新的Paperclip(来自GitHub主分支)和aws-sdk版本2,我通过以下配置解决了我的问题:
require "aws-sdk"
Aws.config[:s3] = {stub_responses: true}

更多信息,请查看Amazon SDK

5

以下内容放置在'my spec/rails_helper.rb'文件中对我有用:

require 'aws'
AWS.stub!
AWS.config(:access_key_id => "TESTKEY", :secret_access_key => "TESTSECRET")

3

许多这些技术似乎无法与最新的paperclip和S3一起使用。对我最终有效的组合是:

AWS.config(:access_key_id => "TESTKEY", :secret_access_key => "TESTSECRET", :stub_requests => true)

并且

Mymodel.any_instance.stubs(:save_attached_files).returns(true)

但实际上,在许多情况下,您只需要使用AWS:stub_requests,就能实现您想要的功能。


我从您的想法开始,并且阅读了Aws :: Client类的v2文档,决定这样做更好:Aws.config = {stub_responses: true} - sameers

3

@Eric M. 你添加了哪些代码使其实际工作?我遇到了同样的问题。 - Peter Nixey
@Eric M. @Eliza @Peter Nixey 这个问题有没有直接的解决方案?我已经尝试过各种策略,包括在不同阶段使用Paperclip::Shoulda,但都没有成功。有什么后续建议吗? - sorens
@sorens - 不好意思,我不知道。我已经放弃了这个。 - Peter Nixey

1
这是我解决的方式。首先,你必须安装fakeweb gem,否则会失败。你还必须在`spec/support/paperclip/[model]/[attachment_name][ext]`路径下有一个空文件。
我所做的是将Paperclip的代码复制并粘贴到我的工厂中。我无法让'paperclip_fixture'起作用。
factory :attachment do
  file do |a|
    # Stubbed  Paperclip attachment from: https://github.com/thoughtbot/paperclip/blob/master/shoulda_macros/paperclip.rb#L68
    # FIX: This was the only way I made this work. Calling the paperclip_fixture directly didn't work.
    # See: https://dev59.com/Zm445IYBdhLWcg3wR4VO
    model, attachment, extension = "customer_attachment", "file", "doc"      
    definition = model.gsub(" ", "_").classify.constantize.
                       attachment_definitions[attachment.to_sym]

    path = "http://s3.amazonaws.com/:id/#{definition[:path]}"
    path.gsub!(/:([^\/\.]+)/) do |match|
      "([^\/\.]+)"
    end

    begin
      FakeWeb.register_uri(:put, Regexp.new(path), :body => "OK")
    rescue NameError
      raise NameError, "the stub_paperclip_s3 shoulda macro requires the fakeweb gem."
    end
    base_path = File.join(Rails.root, "spec", "support", "paperclip")
    File.new(File.join(base_path, model, "#{attachment}.#{extension}"))
  end
end

1

我刚刚在将一个应用从Rails 2.3升级到Rails 3.0时遇到了这个问题,之前我已经按照Testing Paperclip on S3 with Cucumber & Factory Girl的指导进行了测试。

与其将Paperclip::Shoulda模块包含到Factory类中,我需要将它包含到FactoryGirl::DefinitionProxy类中,所以我做了以下更改:

class Factory
  include Paperclip::Shoulda
end

class FactoryGirl::DefinitionProxy
  include Paperclip::Shoulda
end

供参考,我使用的是paperclip 2.4.1和factory_girl 2.0.5。


1
这是我如何在不使用shoulda helpers的情况下为paperclip存根文件的方法。
before(:each) do 
  @sheet = double('sheet')
  @sheet.stub(:url).and_return(File.join(Rails.root, 'spec','fixtures','files', 'file.xls'))
  active_record_object.stub(:sheet).and_return(@sheet)
end

希望这对某人有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接