模型:
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
我希望避免将一个20MB的文件放入仓库中进行测试。是否有类似于我上面尝试的方法,可以真正起作用呢?
shoulda-matchers
,由于显而易见的原因,它们还没有内置validate_size_of
匹配器。另一件事是,恐怕你需要像你提到的那样粗略地编写测试。 - Gaurav Agarwal