Commit 280a85a6 by Joe Ferris

Changed rspec matchers to accept an instance or a class

parent ae97ec66
......@@ -12,6 +12,7 @@ module Paperclip
def matches? subject
@subject = subject
@subject = @subject.class unless Class === @subject
responds? && has_column? && included?
end
......
......@@ -22,6 +22,7 @@ module Paperclip
def matches? subject
@subject = subject
@subject = @subject.class unless Class === @subject
@allowed_types && @rejected_types &&
allowed_types_allowed? && rejected_types_rejected?
end
......
......@@ -12,6 +12,7 @@ module Paperclip
def matches? subject
@subject = subject
@subject = @subject.class unless Class === @subject
error_when_not_valid? && no_error_when_valid?
end
......
......@@ -28,6 +28,7 @@ module Paperclip
def matches? subject
@subject = subject
@subject = @subject.class unless Class === @subject
lower_than_low? && higher_than_low? && lower_than_high? && higher_than_high?
end
......
......@@ -106,3 +106,23 @@ def silence_warnings
ensure
$VERBOSE = old_verbose
end
def should_accept_dummy_class
should "accept the class" do
assert_accepts @matcher, @dummy_class
end
should "accept an instance of that class" do
assert_accepts @matcher, @dummy_class.new
end
end
def should_reject_dummy_class
should "reject the class" do
assert_rejects @matcher, @dummy_class
end
should "reject an instance of that class" do
assert_rejects @matcher, @dummy_class.new
end
end
......@@ -8,14 +8,17 @@ class HaveAttachedFileMatcherTest < Test::Unit::TestCase
@matcher = self.class.have_attached_file(:avatar)
end
should "reject a class with no attachment" do
assert_rejects @matcher, @dummy_class
context "given a class with no attachment" do
should_reject_dummy_class
end
should "accept a class with an attachment" do
modify_table("dummies"){|d| d.string :avatar_file_name }
@dummy_class.has_attached_file :avatar
assert_accepts @matcher, @dummy_class
context "given a class with an attachment" do
setup do
modify_table("dummies"){|d| d.string :avatar_file_name }
@dummy_class.has_attached_file :avatar
end
should_accept_dummy_class
end
end
end
......@@ -14,18 +14,24 @@ class ValidateAttachmentContentTypeMatcherTest < Test::Unit::TestCase
rejecting(%w(audio/mp3 application/octet-stream))
end
should "reject a class with no validation" do
assert_rejects @matcher, @dummy_class
context "given a class with no validation" do
should_reject_dummy_class
end
should "reject a class with a validation that doesn't match" do
@dummy_class.validates_attachment_content_type :avatar, :content_type => %r{audio/.*}
assert_rejects @matcher, @dummy_class
context "given a class with a validation that doesn't match" do
setup do
@dummy_class.validates_attachment_content_type :avatar, :content_type => %r{audio/.*}
end
should_reject_dummy_class
end
should "accept a class with a validation" do
@dummy_class.validates_attachment_content_type :avatar, :content_type => %r{image/.*}
assert_accepts @matcher, @dummy_class
context "given a class with a matching validation" do
setup do
@dummy_class.validates_attachment_content_type :avatar, :content_type => %r{image/.*}
end
should_accept_dummy_class
end
end
end
......@@ -11,13 +11,16 @@ class ValidateAttachmentPresenceMatcherTest < Test::Unit::TestCase
@matcher = self.class.validate_attachment_presence(:avatar)
end
should "reject a class with no validation" do
assert_rejects @matcher, @dummy_class
context "given a class with no validation" do
should_reject_dummy_class
end
should "accept a class with a validation" do
@dummy_class.validates_attachment_presence :avatar
assert_accepts @matcher, @dummy_class
context "given a class with a matching validation" do
setup do
@dummy_class.validates_attachment_presence :avatar
end
should_accept_dummy_class
end
end
end
......@@ -14,37 +14,37 @@ class ValidateAttachmentSizeMatcherTest < Test::Unit::TestCase
context "of limited size" do
setup{ @matcher = self.class.validate_attachment_size(:avatar).in(256..1024) }
should "reject a class with no validation" do
assert_rejects @matcher, @dummy_class
context "given a class with no validation" do
should_reject_dummy_class
end
should "reject a class with a validation that's too high" do
@dummy_class.validates_attachment_size :avatar, :in => 256..2048
assert_rejects @matcher, @dummy_class
context "given a class with a validation that's too high" do
setup { @dummy_class.validates_attachment_size :avatar, :in => 256..2048 }
should_reject_dummy_class
end
should "reject a class with a validation that's too low" do
@dummy_class.validates_attachment_size :avatar, :in => 0..1024
assert_rejects @matcher, @dummy_class
context "given a class with a validation that's too low" do
setup { @dummy_class.validates_attachment_size :avatar, :in => 0..1024 }
should_reject_dummy_class
end
should "accept a class with a validation that matches" do
@dummy_class.validates_attachment_size :avatar, :in => 256..1024
assert_accepts @matcher, @dummy_class
context "given a class with a validation that matches" do
setup { @dummy_class.validates_attachment_size :avatar, :in => 256..1024 }
should_accept_dummy_class
end
end
context "validates_attachment_size with infinite range" do
setup{ @matcher = self.class.validate_attachment_size(:avatar) }
should "accept a class with an upper limit" do
@dummy_class.validates_attachment_size :avatar, :less_than => 1
assert_accepts @matcher, @dummy_class
context "given a class with an upper limit" do
setup { @dummy_class.validates_attachment_size :avatar, :less_than => 1 }
should_accept_dummy_class
end
should "accept a class with no upper limit" do
@dummy_class.validates_attachment_size :avatar, :greater_than => 1
assert_accepts @matcher, @dummy_class
context "given a class with no upper limit" do
setup { @dummy_class.validates_attachment_size :avatar, :greater_than => 1 }
should_accept_dummy_class
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