這就是我如何得到這個工作。首先,你必須擁有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
你能發佈更多有關您的代碼嗎?例如,什麼是a.cover?我假設a是你的對象,而cover是包含圖像URI的屬性? – hahuang65 2011-02-09 10:16:48
另外,你究竟想要測試什麼?這將有助於在這裏獲得更多的上下文。 – hahuang65 2011-02-09 10:17:10