如何使用 Rspec 测试载波版本大小

发布于 2024-11-30 16:56:31 字数 1445 浏览 1 评论 0原文

我有一个 CarrierWave::Uploader 可以生成三个版本的上传图像。

# Process files as they are uploaded:                                                                  
   process :resize_to_fit => [400, 400]                                                                   

   # Create different versions of your uploaded files:                                                    
   version :thumb do                                                                                      
     process :resize_to_fit => [60, 60]
   end

   version :small do
     process :resize_to_fit => [24, 24]
   end

在我的测试中,我尝试验证生成图像的尺寸

require 'spec_helper'
require 'carrierwave/test/matchers'

describe 'manufacturer logo uploader' do
  include CarrierWave::Test::Matchers

  before(:each) do
    image_path = Rails.root.join('test/fixtures/images', 'avatar100.gif').to_s
    @manufacturer = Factory.create(:manufacturer, :page_status => 1)
    @manufacturer.logo_image = File.open(image_path)
    @manufacturer.save!
  end

  context "manufacturer logo dimensions" do
    it "should have three versions" do
      @manufacturer.logo_image.should have_dimensions(400,400)
      @manufacturer.logo_image.thumb.should have_dimensions(60,60)
      @manufacturer.logo_image.small.should have_dimensions(24,24)
    end
  end

end

,但此测试取决于实际图像,并且 resize_to_fit 不一定会将其大小调整为指定尺寸。关于如何使用存根测试这个有什么想法吗?

I have a CarrierWave::Uploader that produces three version of the uploaded image.

# Process files as they are uploaded:                                                                  
   process :resize_to_fit => [400, 400]                                                                   

   # Create different versions of your uploaded files:                                                    
   version :thumb do                                                                                      
     process :resize_to_fit => [60, 60]
   end

   version :small do
     process :resize_to_fit => [24, 24]
   end

And in my tests I try to verify the dimensions of the generated images

require 'spec_helper'
require 'carrierwave/test/matchers'

describe 'manufacturer logo uploader' do
  include CarrierWave::Test::Matchers

  before(:each) do
    image_path = Rails.root.join('test/fixtures/images', 'avatar100.gif').to_s
    @manufacturer = Factory.create(:manufacturer, :page_status => 1)
    @manufacturer.logo_image = File.open(image_path)
    @manufacturer.save!
  end

  context "manufacturer logo dimensions" do
    it "should have three versions" do
      @manufacturer.logo_image.should have_dimensions(400,400)
      @manufacturer.logo_image.thumb.should have_dimensions(60,60)
      @manufacturer.logo_image.small.should have_dimensions(24,24)
    end
  end

end

but this test depends on the actual image and resize_to_fit will not necessarily resize it to the specified dimensions. Any ideas on how to test this using stubs?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最初的梦 2024-12-07 16:56:31

这是我的解决方案,它实际上处理图像。这比存根慢,但会验证实际的调整大小(只要输入图像大于目标大小)。

describe 'images' do

  include CarrierWave::Test::Matchers

  before do
    MyUploader.enable_processing = true
  end

  it 'are resized' do
    path = Rails.root.join *%w[ spec data sample.png ]
    my_model = FactoryGirl.create :my_model, image: path.open

    my_model.artwork.small.should be_no_larger_than(300, 400)
  end

  after do
    MyUploader.enable_processing = false
  end

end

Here's my solution, which actually processes an image. This is slower than stubs, but verifies the actual resize (as long as the input image is larger than the target size).

describe 'images' do

  include CarrierWave::Test::Matchers

  before do
    MyUploader.enable_processing = true
  end

  it 'are resized' do
    path = Rails.root.join *%w[ spec data sample.png ]
    my_model = FactoryGirl.create :my_model, image: path.open

    my_model.artwork.small.should be_no_larger_than(300, 400)
  end

  after do
    MyUploader.enable_processing = false
  end

end
空心空情空意 2024-12-07 16:56:31

远景,但您可以尝试添加此选项

before do
  DocumentUploader.enable_processing = true
end

,因为出于性能原因,默认情况下可以关闭处理(当前版本和其他版本),

也有与 process set_file_name_to_model 相关的类似问题,该问题正在执行设置“file_name”的操作模型属性

http://ruby-on-rails-eq8.blogspot.co.uk/2015/03/rierwave-uploader-not-triggering.html

long shot but can you try to add this

before do
  DocumentUploader.enable_processing = true
end

because processing (current version and other versions) could be turned off by default for performance reason

had similar problem related to process set_file_name_to_model that was doing something setting "file_name" on model attribute

http://ruby-on-rails-eq8.blogspot.co.uk/2015/03/carrierwave-uploader-not-triggering.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文