【问题标题】:Test paperclip image styles with Rspec使用 Rspec 测试回形针图像样式
【发布时间】:2012-01-25 19:45:11
【问题描述】:

我有一个带有附件的横幅,使用回形针。 Paperclip 将使用“styles”参数为上传的图像创建“衍生物”:

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

我想测试这个 attach_file 生成,最好不要测试到磁盘的整个往返过程等等。如果创建了具有特定尺寸的某个文件,我可以创建一个横幅,附加一个文件,然后查看文件系统,但这与我测试重点部分而不测试整个堆栈的想法背道而驰。

如何测试附件:image在回形针中是否有特定样式?

【问题讨论】:

    标签: ruby-on-rails rspec paperclip


    【解决方案1】:

    has_attached_file 方法所做的只是将传递的定义添加到模型中。生成是通过回形针完成的,不需要测试。

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

    但是,您可以使用attachment_definitions 方法测试定义是否正确创建。像这样的:

    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
    

    【讨论】:

    • 谢谢。我知道我不需要测试回形针;但是当BDD-ing时,我想先写一个像it "should have a thumbnail" do这样的测试,然后再实现它。
    • 我有空的 attachment_definitions[:data]。所以我使用 Attachment.attachment_definitions[:attached_file][:styles][:small] 构造让它工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多