如何在Rspec中存根载波?

时间:2021-02-09 20:23:23

I want to stub carrierwave to prevent it from fetch images on the web during my tests. How would I stub things to achieve this?

我想在我的测试中使用stubwave来阻止它在网上获取图像。我将如何实现这一目标?

My crawler parses a remote web page, and saves one image url into the model. Carrierwave will fetch that image automatically during the save operation. It works well.

我的抓取工具解析远程网页,并将一个图像网址保存到模型中。 Carrierwave将在保存操作期间自动获取该图像。它运作良好。

However I have a test about the parsing of pages, and every-time it will download the file, which slows down the testing.

但是我有一个关于页面解析的测试,并且每次都会下载文件,这会减慢测试速度。

UPDATE:

更新:

I mount the uploader as the following (in the pre-existing paperclip column)

我按以下方式安装上传器(在预先存在的回形针列中)

mount_uploader :image, TopicImageUploader, :mount_on => :image_file_name

I tried to stub the following, but neither worked:

我试图将以下内容存根,但都没有奏效:

Topic.any_instance.stub(:store_image!)
Topic.any_instance.stub(:store_image_file_name!)
Topic.any_instance.stub(:store_image_remote_url!)

4 个解决方案

#1


14  

TopicImageUploader.any_instance.stub(:download!)

#2


9  

This is what I'm using in my spec_helper:

这是我在spec_helper中使用的内容:

class CarrierWave::Mount::Mounter
  def store!
  end
end

This completely blocks all real file uploads (note that I'm using this with carrier wave 0.5.8, which is newest version at the time of writing, if you're using much older version, it might differ). If you want to control tests which stub uploads, you could use:

这完全阻止了所有真正的文件上传(注意我使用的是载波0.5.8,这是写作时的最新版本,如果你使用的是更旧的版本,它可能会有所不同)。如果要控制哪些存根上传的测试,您可以使用:

CarrierWave::Mount::Mounter.any_instance.stub(:store!)

#3


4  

I reduced my test-suite time from 25 seconds to just 2 seconds with a simple config in the CarrierWave initializer:

通过CarrierWave初始化程序中的简单配置,我将测试套件时间从25秒减少到仅仅2秒:

# config/initializers/carrier_wave.rb
CarrierWave.configure do |config|
  config.enable_processing = false if Rails.env.test?
end

This config skips the image manipulation (resizing, cropping, ...) of ImageMagick, MiniMagick ect.

此配置会跳过ImageMagick,MiniMagick等的图像处理(调整大小,裁剪...)。

#4


0  

allow_any_instance_of(CarrierWave::Uploader::Base).to receive(:store!).and_return nil

#1


14  

TopicImageUploader.any_instance.stub(:download!)

#2


9  

This is what I'm using in my spec_helper:

这是我在spec_helper中使用的内容:

class CarrierWave::Mount::Mounter
  def store!
  end
end

This completely blocks all real file uploads (note that I'm using this with carrier wave 0.5.8, which is newest version at the time of writing, if you're using much older version, it might differ). If you want to control tests which stub uploads, you could use:

这完全阻止了所有真正的文件上传(注意我使用的是载波0.5.8,这是写作时的最新版本,如果你使用的是更旧的版本,它可能会有所不同)。如果要控制哪些存根上传的测试,您可以使用:

CarrierWave::Mount::Mounter.any_instance.stub(:store!)

#3


4  

I reduced my test-suite time from 25 seconds to just 2 seconds with a simple config in the CarrierWave initializer:

通过CarrierWave初始化程序中的简单配置,我将测试套件时间从25秒减少到仅仅2秒:

# config/initializers/carrier_wave.rb
CarrierWave.configure do |config|
  config.enable_processing = false if Rails.env.test?
end

This config skips the image manipulation (resizing, cropping, ...) of ImageMagick, MiniMagick ect.

此配置会跳过ImageMagick,MiniMagick等的图像处理(调整大小,裁剪...)。

#4


0  

allow_any_instance_of(CarrierWave::Uploader::Base).to receive(:store!).and_return nil