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
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
# File lib/paperclip/matchers/have_attached_file_matcher.rb, line 11 11: def have_attached_file name 12: HaveAttachedFileMatcher.new(name) 13: end
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
# File lib/paperclip/matchers/validate_attachment_content_type_matcher.rb, line 13 13: def validate_attachment_content_type name 14: ValidateAttachmentContentTypeMatcher.new(name) 15: end
Ensures that the given instance or class validates the presence of the given attachment.
describe User do
it { should validate_attachment_presence(:avatar) }
end
# File lib/paperclip/matchers/validate_attachment_presence_matcher.rb, line 10 10: def validate_attachment_presence name 11: ValidateAttachmentPresenceMatcher.new(name) 12: end
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) }
# File lib/paperclip/matchers/validate_attachment_size_matcher.rb, line 14 14: def validate_attachment_size name 15: ValidateAttachmentSizeMatcher.new(name) 16: end