【问题标题】:Stubbing Paperclip S3 requests in specs在规范中存根回形针 S3 请求
【发布时间】:2011-06-23 22:01:36
【问题描述】:

我正在使用 Paperclip 和 S3 进行图像上传,并试图从我的测试套件中剔除对 S3 的调用。我找到了提到做的思想机器人帖子

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

但这给了我“错误数量的参数(4 对 2)”错误。我尝试将上面的参数切换到一个数组,这消除了原始错误,但给出了一个错误,提示“属性已定义:paperclip_fixture”。

有没有人能够让这个工作?此外,理想情况下,我希望将本地文件系统用于开发环境。有没有简单的方法可以做到这一点?

【问题讨论】:

  • 你能发布更多关于你的代码的信息吗?例如,什么是a.cover?我假设 a 是您的对象,而 cover 是包含图像 URI 的属性?
  • 另外,你到底想测试什么?在这里有更多的上下文会有所帮助。

标签: ruby-on-rails amazon-s3 paperclip stubbing


【解决方案1】:

使用最新的回形针(来自 github master 分支)和 aws-sdk 版本 2,我通过以下配置解决了我的问题:

require "aws-sdk"
Aws.config[:s3] = {stub_responses: true}

更多信息请关注amazon sdk

【讨论】:

    【解决方案2】:

    将它放在我的“spec/rails_helper.rb”文件中对我有用:

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

    【讨论】:

      【解决方案3】:

      在关注Testing Paperclip on S3 with Cucumber & Factory Girl 之后,我刚刚将应用程序从 Rails 2.3 升级到 Rails 3.0。

      我没有将 Paperclip::Shoulda 模块包含到 Factory 类中,而是将其包含到 FactoryGirl::DefinitionProxy 类中,所以我更改了这个:

      class Factory
        include Paperclip::Shoulda
      end
      

      class FactoryGirl::DefinitionProxy
        include Paperclip::Shoulda
      end
      

      作为参考,我使用的是回形针 2.4.1 和 factory_girl 2.0.5。

      【讨论】:

        【解决方案4】:

        其中许多技术似乎不适用于最新的回形针和 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,它会实现您想要的。

        【讨论】:

        【解决方案5】:

        这就是我在不使用 shoulda 助手的情况下从回形针中存根文件的方法。

        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
        

        希望这对某人有所帮助。

        【讨论】:

          【解决方案6】:

          这就是我的工作方式。首先,您必须拥有 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: http://stackoverflow.com/questions/4941586/stubbing-paperclip-s3-requests-in-specs
              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
          

          【讨论】:

            【解决方案7】:

            好的,我已经解决了基本问题。这(我相信)正如 Eliza 所说,因为我没有使用 shoulda(我使用的是 rspec 2.6.0factory_girl 2.1.2)。

            这对我有用(Profile 是具有附件的类):

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

            目前,我在 before 示例的 before 方法中拥有此权利。可能有更好的放置位置。

            【讨论】:

              【解决方案8】:

              你在用应该吗?如果您不应该使用您正在使用的 paperclip_fixture 方法,则可能来自其他地方,因此行为会有所不同。

              可能相关:https://github.com/thoughtbot/paperclip/blob/master/shoulda_macros/paperclip.rb

              【讨论】:

              • @Eric M. 您添加了哪些代码来实际完成这项工作?我也遇到了同样的问题
              • @Eric M. @Eliza @Peter Nixey 这个问题有没有直接的解决方案?我尝试了各种策略,包括在各个阶段Paperclip::Shoulda,但没有运气。有什么后续建议吗?
              • @sorens - 不知道我害怕。我从这里继续前进
              猜你喜欢
              • 2015-07-08
              • 1970-01-01
              • 2018-04-24
              • 1970-01-01
              • 2015-03-24
              • 2016-11-08
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多