[英]Paperclip 4, Amazon S3 & rspec: how to stub file upload?
I'm writing tests with rspec and am a bit struggling with Paperclip 4. At the moment I'm using webmock to stub requests but image processing is slowing tests down. 我正在用rspec编写测试,并且我对Paperclip 4有点挣扎。目前我正在使用webmock来存根请求,但图像处理正在减慢测试速度。
Everything I read suggest to use stubs like so: 我读到的所有内容都建议使用这样的存根:
Profile.any_instance.stub(:save_attached_files).and_return(true)
It doesn't work since :save_attached_files disappeared with Paperclip 4 as far as I can tell. 它不起作用:据我所知,save_attached_files在Paperclip 4中消失了。
What's the proper way to do it now ? 现在这样做的正确方法是什么?
Thanks 谢谢
Add this to your rails_helper.rb
in your config block: 将其添加到配置块中的
rails_helper.rb
:
RSpec.configure do |config|
# Stub saving of files to S3
config.before(:each) do
allow_any_instance_of(Paperclip::Attachment).to receive(:save).and_return(true)
end
end
It doesn't exactly answer my question, but I found a way to run faster specs thanks to this dev blog , so I'm posting it if it can help someone else. 它并没有完全回答我的问题,但我找到了一种方法来运行更快的规格,这要归功于这个开发博客 ,所以如果它可以帮助其他人我发布它。
Just add this piece of code at the beginning of your spec file or in a helper. 只需在spec文件的开头或帮助器中添加这段代码即可。 My tests are running 3x faster now.
我的测试现在运行速度提高了3倍。
# We stub some Paperclip methods - so it won't call shell slow commands
# This allows us to speedup paperclip tests 3-5x times.
module Paperclip
def self.run cmd, params = "", expected_outcodes = 0
cmd == 'convert' ? nil : super
end
end
class Paperclip::Attachment
def post_process
end
end
For newer versions of Paperclip, use the following: 对于较新版本的Paperclip,请使用以下内容:
module Paperclip
def self.run cmd, arguments = "", interpolation_values = {}, local_options = {}
cmd == 'convert' ? nil : super
end
end
class Paperclip::Attachment
def post_process
end
end
I had a heckuva time dealing with this. 我有一个heckuva时间处理这个问题。 I didn't want to test Paperclip per se -- rather, I needed a file to exist on my model so I can test a very sensitive custom class.
我不想测试Paperclip本身 - 相反,我需要一个文件存在于我的模型上,所以我可以测试一个非常敏感的自定义类。
None of the other answers worked for me (Rails 5, Rspec 3.5) so I gave up on stubbing AWS or using the Paperclip::Shoulda::Matchers. 其他答案都没有对我有用(Rails 5,Rspec 3.5)所以我放弃了对AWS进行存根或使用Paperclip :: Shoulda :: Matchers。 Nothing worked and it ate up an entire day!
什么都没有用,它吃了一整天! Finally I gave up on Paperclip altogether and went this route:
最后我放弃了Paperclip并走了这条路:
list = build(:list)
allow(list).to receive_message_chain("file.url") { "#{Rails.root}/spec/fixtures/guess_fullname.csv" }
importer = Importer.new(list)
expect(importer.set_key_mapping).to eq({nil=>:email_address, :company=>:company, :fullname=>:full_name})
Where my models/list.rb
has has_attached_file :file
and Importer is a class I wrote that takes the file, which was already uploaded to S3 by paperclip before the class is initialized, and processes it. 我的
models/list.rb
有has_attached_file :file
和Importer是我编写的一个类,它接收了在初始化类之前已经通过paperclip上传到S3的文件,并对其进行处理。 The file.url
would otherwise be the S3 URL, so I give it the path in my repo to a testing csv file. 否则
file.url
将是S3 URL,因此我将我的repo中的路径提供给测试csv文件。 set_key_mapping
is a method in my class that I can use to verify the processing part worked. set_key_mapping
是我班上的一个方法,我可以用它来验证处理部分是否有效。
Hope this saves somebody a few hours... 希望这能节省几个小时......
What about adding AWS.stub!
那么添加
AWS.stub!
in your spec/spec_helper.rb
? 在您的
spec/spec_helper.rb
? Give that a try. 试一试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.