3
模型:如何使用Rspec来测试使用Paperclip的模型是否验证上传文件的大小?
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的文件到回购只是为了测试这一点。有没有类似于我上面尝试过的那种方法实际上可行?
添加一个模拟的文件,它的功能大小应该做的伎俩 – scones 2013-02-09 14:25:04
我猜你正在使用'早该-matchers'不已经有validate_size_of匹配内置的原因很明显。 第二件事是,我担心你会需要像你提到的那样粗暴地写测试。 – 2013-02-09 14:25:35