gpt4 book ai didi

ruby-on-rails-3 - 如何使用 Rspec 测试载波版本大小

转载 作者:行者123 更新时间:2023-12-01 04:09:31 25 4
gpt4 key购买 nike

我有一个 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 不一定会将其调整为指定的尺寸。关于如何使用 stub 测试这个的任何想法?

最佳答案

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

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

关于ruby-on-rails-3 - 如何使用 Rspec 测试载波版本大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7158808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com