Module: Paperclip::Shoulda::Matchers
- Defined in:
- lib/paperclip/matchers.rb,
lib/paperclip/matchers/have_attached_file_matcher.rb,
lib/paperclip/matchers/validate_attachment_size_matcher.rb,
lib/paperclip/matchers/validate_attachment_presence_matcher.rb,
lib/paperclip/matchers/validate_attachment_content_type_matcher.rb
Overview
Provides RSpec-compatible & Test::Unit-compatible matchers for testing Paperclip attachments.
RSpec
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 (:avatar) }
it { should (:avatar).
allowing('image/png', 'image/gif').
rejecting('text/plain', 'text/xml') }
it { should (:avatar).
less_than(2.megabytes) }
end
TestUnit
In test_helper.rb, you’ll need to require the matchers as well:
require "paperclip/matchers"
And extend the module:
class ActiveSupport::TestCase
extend Paperclip::Shoulda::Matchers
#...other initializers...#
end
Example:
require 'test_helper'
class UserTest < ActiveSupport::TestCase
should have_attached_file(:avatar)
should (:avatar)
should (:avatar).
allowing('image/png', 'image/gif').
rejecting('text/plain', 'text/xml')
should (:avatar).
less_than(2.megabytes)
end
Defined Under Namespace
Classes: HaveAttachedFileMatcher, ValidateAttachmentContentTypeMatcher, ValidateAttachmentPresenceMatcher, ValidateAttachmentSizeMatcher
Instance Method Summary collapse
-
#have_attached_file(name) ⇒ Object
Ensures that the given instance or class has an attachment with the given name.
-
#validate_attachment_content_type(name) ⇒ Object
Ensures that the given instance or class validates the content type of the given attachment as specified.
-
#validate_attachment_presence(name) ⇒ Object
Ensures that the given instance or class validates the presence of the given attachment.
-
#validate_attachment_size(name) ⇒ Object
Ensures that the given instance or class validates the size of the given attachment as specified.
Instance Method Details
#have_attached_file(name) ⇒ Object
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
11 12 13 |
# File 'lib/paperclip/matchers/have_attached_file_matcher.rb', line 11 def have_attached_file name HaveAttachedFileMatcher.new(name) end |
#validate_attachment_content_type(name) ⇒ Object
Ensures that the given instance or class validates the content type of the given attachment as specified.
Example:
describe User do
it { should (:icon).
allowing('image/png', 'image/gif').
rejecting('text/plain', 'text/xml') }
end
13 14 15 |
# File 'lib/paperclip/matchers/validate_attachment_content_type_matcher.rb', line 13 def name ValidateAttachmentContentTypeMatcher.new(name) end |
#validate_attachment_presence(name) ⇒ Object
Ensures that the given instance or class validates the presence of the given attachment.
describe User do
it { should (:avatar) }
end
10 11 12 |
# File 'lib/paperclip/matchers/validate_attachment_presence_matcher.rb', line 10 def name ValidateAttachmentPresenceMatcher.new(name) end |
#validate_attachment_size(name) ⇒ Object
Ensures that the given instance or class validates the size of the given attachment as specified.
Examples:
it { should (:avatar).
less_than(2.megabytes) }
it { should (:icon).
greater_than(1024) }
it { should (:icon).
in(0..100) }
14 15 16 |
# File 'lib/paperclip/matchers/validate_attachment_size_matcher.rb', line 14 def name ValidateAttachmentSizeMatcher.new(name) end |