Commit e8518e0e by Jon Yurek

New shoulda macros that are actually useful

parent e8f45902
......@@ -8,20 +8,60 @@ module Paperclip
assert klass.instance_methods.include?(meth), "#{klass.name} does not respond to #{name}."
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 "with an attachment that is #{max+1} bytes" do
setup do
@file = StringIO.new("." * (max+1))
@attachment = klass.new.send(name)
@attachment.assign(@file)
end
should "have the correct definition" do
expected = options
actual = klass.attachment_definitions[name]
expected.delete(:validations) if not options.key?(:validations)
expected.delete(:whiny_thumbnails) if not options.key?(:whiny_thumbnails)
should "have a :size validation error" do
assert @attachment.errors[:size]
end
end
context "with an attachment that us #{max-1} bytes" do
setup do
@file = StringIO.new("." * (max-1))
@attachment = klass.new.send(name)
@attachment.assign(@file)
end
assert_equal expected, actual
should "not have a :size validation error" do
assert ! @attachment.errors[:size]
end
end
should "ensure that ImageMagick is available" do
%w( convert identify ).each do |command|
`#{Paperclip.path_for_command(command)}`
assert_equal 0, $?, "ImageMagick's #{command} returned with an error. Make sure that #{command} is available at #{Paperclip.path_for_command(command)}"
if min > 0
context "with an attachment that is #{min-1} bytes" do
setup do
@file = StringIO.new("." * (min-1))
@attachment = klass.new.send(name)
@attachment.assign(@file)
end
should "have a :size validation error" do
assert @attachment.errors[:size]
end
end
context "with an attachment that us #{min+1} bytes" do
setup do
@file = StringIO.new("." * (min+1))
@attachment = klass.new.send(name)
@attachment.assign(@file)
end
should "not have a :size validation error" do
assert ! @attachment.errors[:size]
end
end
end
end
......
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