简体   繁体   English

使用Rspec测试回形针图像样式

[英]Test paperclip image styles with Rspec

I have a Banner, with an attached file, using paperclip. 我有一个带有附件的横幅,使用回形针。 Paperclip will create "derivatives" for an uploaded image, using the "styles" parameter: Paperclip将使用“styles”参数为上传的图像创建“衍生物”:

class Banner < ActiveRecord::Base
  has_attached_file :image,
                    :styles => {:medium => "300x300>",
                                :thumb  => "100x100>"}
end

I want to test this attached_file generation, preferably without testing the whole roundtrip to a disk and so on. 我想测试这个attach_file生成,最好不要测试整个往返到磁盘等等。 I could create a Banner, attach a file and then look on the filesystem if a certain file with certain dimensions is created, but that goes against my idea of testing a focused part, without testing the entire stack. 我可以创建一个Banner,附加一个文件然后查看文件系统,如果创建了某个具有特定尺寸的文件,但这违背了我测试焦点部分的想法,而不测试整个堆栈。

How can I test if the attached file :image has certain styles in Paperclip? 如何测试附加文件:image在Paperclip中是否具有某些样式?

All the has_attached_file method does is add the passed definitions to the model. 所有has_attached_file方法都将传递的定义添加到模型中。 The generation is done by paperclip and shouldn't need to be tested. 这一代是通过回形针完成的,不需要进行测试。

Source: https://github.com/thoughtbot/paperclip/blob/master/lib/paperclip.rb#L174 资料来源: https//github.com/thoughtbot/paperclip/blob/master/lib/paperclip.rb#L174

However, you could test that the definitions were created properly using the attachment_definitions method. 但是,您可以使用attachment_definitions方法测试是否正确创建了定义。 Something like this: 像这样的东西:

it "has correct image style geometry" do
   Banner.attachment_definitions[:data][:styles][:medium][:geometry].should == "300x300>"
   Banner.attachment_definitions[:data][:styles][:thumb][:geometry].should == "100x100>"
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 图像的回形针 styles 中的 # 和 &gt; 之间的区别 - Difference between # and > in paperclip styles for image RSpec + Paperclip:识别:此图像格式没有解码代理 - RSpec + Paperclip: identify: no decode delegate for this image format 在rspec中使用post请求传递图像(回形针) - passing image with post request in rspec (paperclip) Paperclip中的样式只有在它是图像的情况下[rails] - Styles in Paperclip only if it's an image [rails] 如何使用Rails,Paperclip和RSpec请求规范测试文件下载? - How to test file download with Rails, Paperclip and RSpec request spec? Paperclip / Rspec测试:有没有更快的方法来测试paperclip validates_attachment_content_type? - Paperclip/Rspec tests: Is there a faster way to test paperclip validates_attachment_content_type? 使用Rspec和Paperclip在模型规格中附加并验证图像 - Attach and validate an image in model specs using Rspec and Paperclip Rails和Paperclip ...不保存原始图像,只保存样式? - Rails and Paperclip… don't save the original image, just the styles? 回形针不生成样式,原始图像显示为残破 - Paperclip don't generate styles and original image appears as broken 阻止在Paperclip中的开发/测试中将图像上载到AWS - Prevent image upload to AWS in development/test in Paperclip
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM