如何使用 Rspec 测试载波版本大小
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我的解决方案,它实际上处理图像。这比存根慢,但会验证实际的调整大小(只要输入图像大于目标大小)。
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).
远景,但您可以尝试添加此选项
,因为出于性能原因,默认情况下可以关闭处理(当前版本和其他版本),
也有与
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
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 attributehttp://ruby-on-rails-eq8.blogspot.co.uk/2015/03/carrierwave-uploader-not-triggering.html