模特:
class Attachment < ActiveRecord::Base
belongs_to :narrative
attr_accessible :description, :user_id, :narrative_id
has_attached_file :file
validates_presence_of :user_id
validates_presence_of :narrative_id
validates_attachment :file, :presence => true,
:size => {:less_than => 20.megabytes}
end
不起作用的测试:
describe Attachment do
it { should validate_presence_of :file }
it { should validate_size_of :file } # validate_size_of does not exist
end
我想避免将20 MB文件转储到repo中来测试它。有没有类似于我上面尝试过的方法才能真正起作用?
答案 0 :(得分:6)
我这样做的最好方法是使用内置的shoulda matchers for Paperclip。该链接的文档非常好,但是这里有一个关于你可以用它做什么的文档的概述:
在spec_helper.rb中,您需要要求匹配器:
require "paperclip/matchers"
并包含模块:
Spec::Runner.configure do |config|
config.include Paperclip::Shoulda::Matchers
end
验证附件大小的示例:
describe User do
it { should validate_attachment_size(:avatar).
less_than(2.megabytes) }
end
如果您有兴趣,匹配器的来源可以是found on GitHub