Commit 17a03b28 by Jon Yurek

Presence and conten_type validation shoulda macros

parent e8518e0e
......@@ -11,12 +11,74 @@ module Paperclip
end
end
def should_validate_attachment_presence name
klass = self.name.gsub(/Test$/, '').constantize
context "Class #{klass.name} validating presence on #{name}" do
context "when the assignment is nil" do
setup do
@attachment = klass.new.send(name)
@attachment.assign(nil)
end
should "have a :presence validation error" do
assert @assignment.errors[:presence]
end
end
context "when the assignment is valid" do
setup do
@attachment = klass.new.send(name)
@attachment.assign(nil)
end
should "have a :presence validation error" do
assert ! @assignment.errors[:presence]
end
end
end
end
def should_validate_attachment_content_type name, options = {}
klass = self.name.gsub(/Test$/, '').constantize
valid = [options[:valid]].flatten
invalid = [options[:invalid]].flatten
context "Class #{klass.name} validating content_types on #{name}" do
valid.each do |type|
context "being assigned a file with a content_type of #{type}" do
setup do
@file = StringIO.new(".")
class << @file; attr_accessor :content_type; end
@file.content_type = type
@attachment = klass.new.send(name)
@attachment.assign(@file)
end
should "not have a :content_type validation error" do
assert ! @assignment.errors[:content_type]
end
end
end
invalid.each do |type|
context "being assigned a file with a content_type of #{type}" do
setup do
@file = StringIO.new(".")
class << @file; attr_accessor :content_type; end
@file.content_type = type
@attachment = klass.new.send(name)
@attachment.assign(@file)
end
should "have a :content_type validation error" do
assert @assignment.errors[:content_type]
end
end
end
end
end
def should_validate_attachment_size name, options = {}
klass = self.name.gsub(/Test$/, '').constantize
min = options[:greater_than] || (options[:in] && options[:in].first) || 0
max = options[:less_than] || (options[:in] && options[:in].last) || (1.0/0)
range = (min..max)
context "Class #{klass.name} with attachment #{name}" do
context "Class #{klass.name} validating file size on #{name}" do
context "with an attachment that is #{max+1} bytes" do
setup do
@file = StringIO.new("." * (max+1))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment