Skip to content

Commit

Permalink
Added documentation for rspec matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
jferris committed Mar 17, 2010
1 parent 280a85a commit 4c6cf39
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ NOTE: Post processing will not even *start* if the attachment is not valid
according to the validations. Your callbacks and processors will *only* be
called with valid attachments.

==Testing

Paperclip provides rspec-compatible matchers for testing attachments. See the
documentation on Paperclip::Shoulda::Matchers for more information.

==Contributing

If you'd like to contribute a feature or bugfix: Thanks! To make sure your
Expand Down
29 changes: 29 additions & 0 deletions lib/paperclip/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,32 @@
require 'paperclip/matchers/validate_attachment_presence_matcher'
require 'paperclip/matchers/validate_attachment_content_type_matcher'
require 'paperclip/matchers/validate_attachment_size_matcher'

module Paperclip
module Shoulda
# Provides rspec-compatible matchers for testing Paperclip attachments.
#
# In spec_helper.rb, you'll need to require the matchers:
#
# require "paperclip/matchers"
#
# And include the module:
#
# Spec::Runner.configure do |config|
# config.include Paperclip::Shoulda::Matchers
# end
#
# Example:
# describe User do
# it { should have_attached_file(:avatar) }
# it { should validate_attachment_presence(:avatar) }
# it { should validate_attachment_content_type(:avatar).
# allowing('image/png', 'image/gif').
# rejecting('text/plain', 'text/xml') }
# it { should validate_attachment_size(:avatar).
# less_than(2.megabytes) }
# end
module Matchers
end
end
end
7 changes: 7 additions & 0 deletions lib/paperclip/matchers/have_attached_file_matcher.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
module Paperclip
module Shoulda
module Matchers
# Ensures that the given instance or class has an attachment with the
# given name.
#
# Example:
# describe User do
# it { should have_attached_file(:avatar) }
# end
def have_attached_file name
HaveAttachedFileMatcher.new(name)
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
module Paperclip
module Shoulda
module Matchers
# Ensures that the given instance or class validates the content type of
# the given attachment as specified.
#
# Example:
# describe User do
# it { should validate_attachment_content_type(:icon).
# allowing('image/png', 'image/gif').
# rejecting('text/plain', 'text/xml') }
# end
def validate_attachment_content_type name
ValidateAttachmentContentTypeMatcher.new(name)
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
module Paperclip
module Shoulda
module Matchers
# Ensures that the given instance or class validates the presence of the
# given attachment.
#
# describe User do
# it { should validate_attachment_presence(:avatar) }
# end
def validate_attachment_presence name
ValidateAttachmentPresenceMatcher.new(name)
end
Expand Down
10 changes: 10 additions & 0 deletions lib/paperclip/matchers/validate_attachment_size_matcher.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
module Paperclip
module Shoulda
module Matchers
# Ensures that the given instance or class validates the size of the
# given attachment as specified.
#
# Examples:
# it { should validate_attachment_size(:avatar).
# less_than(2.megabytes) }
# it { should validate_attachment_size(:icon).
# greater_than(1024) }
# it { should validate_attachment_size(:icon).
# in(0..100) }
def validate_attachment_size name
ValidateAttachmentSizeMatcher.new(name)
end
Expand Down

0 comments on commit 4c6cf39

Please sign in to comment.