2011-02-09 45 views
11

我正在使用Paperclip和S3進行圖像上傳,並試圖從我的測試套件中取消對S3的調用。我發現thoughtbot帖子裏面提到做在規範中存儲回形針S3請求

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

但給了我一個「錯誤的參數數目(4 2)」的錯誤。我嘗試將上述參數切換到一個數組,該數組刪除了原始錯誤,但給出了一個錯誤,指出「屬性已定義:paperclip_fixture」。

有沒有人能夠得到這個工作?另外,我最好喜歡在開發環境中使用本地文件系統。是否有捷徑可尋?

+0

你能發佈更多有關您的代碼嗎?例如,什麼是a.cover?我假設a是你的對象,而cover是包含圖像URI的屬性? – hahuang65 2011-02-09 10:16:48

+0

另外,你究竟想要測試什麼?這將有助於在這裏獲得更多的上下文。 – hahuang65 2011-02-09 10:17:10

回答

3

你在使用嗎?如果您沒有使用shoulda,您正在使用的paperclip_fixture方法可能來自其他地方,因此行爲會有所不同。

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

+0

@Eric M.你添加了哪些代碼來實際完成這項工作?我遇到了同樣的問題 – 2011-11-02 12:46:45

+0

@Eric M. @Eliza @Peter Nixey對這個問題有一個簡單的解決方案嗎?我已經嘗試了各種策略,包括在'回形針:: Shoulda'在各個階段沒有運氣。任何後續建議? – sorens 2013-03-02 15:46:56

+0

@sorens - 不知道我害怕。我從這 – 2013-03-08 15:08:10

7

好吧,我已經得到了基本的問題想通了。這是(我相信),因爲我不使用shoulda(我使用rspec 2.6.0factory_girl 2.1.2),因爲伊麗莎說。

下面是我工作(其中Profile是具有附着物類):

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

此刻我只是在我的我的rspec例如before方法這一權利。這可能是一個更好的地方。

1

這就是我如何得到這個工作。首先,你必須擁有faweweb寶石,否則將失敗。您還必須在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 
1

這就是我如何從paperclip存根文件,而不使用shoulda helpers。

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 

希望這可以幫助別人。

3

許多這些技術似乎不適用於最新的回形針和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,它會實現你想要的。

1

我跟着Testing Paperclip on S3 with Cucumber & Factory Girl後,剛剛遇到這個升級應用程序從Rails 2.3到Rails 3.0。

而是包括回形針::早該模塊到工廠類的,我不得不把它列入到FactoryGirl :: DefinitionProxy課,所以我改變了這一點:

class Factory 
    include Paperclip::Shoulda 
end 

class FactoryGirl::DefinitionProxy 
    include Paperclip::Shoulda 
end 

僅供參考,我使用回形針2.4.1和factory_girl 2.0.5。

5

在我的「投機/ rails_helper.rb」文件放上工作對我來說:

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

隨着最新的回形針(從GitHub主分支)和AWS-SDK 2.0版本,我解決我的問題有以下配置:

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

欲瞭解更多信息,請看一看amazon sdk