Module: Remarkable::MongoMapper::Matchers

Defined in:
lib/remarkable/mongo_mapper/matchers/have_key_matcher.rb,
lib/remarkable/mongo_mapper/matchers/association_matcher.rb,
lib/remarkable/mongo_mapper/matchers/allow_values_for_matcher.rb,
lib/remarkable/mongo_mapper/matchers/validate_length_of_matcher.rb,
lib/remarkable/mongo_mapper/matchers/validate_presence_of_matcher.rb,
lib/remarkable/mongo_mapper/matchers/validate_confirmation_of_matcher.rb

Defined Under Namespace

Classes: AllowValuesForMatcher, AssociationMatcher, HaveKeyMatcher, ValidateConfirmationOfMatcher, ValidateLengthOfMatcher, ValidatePresenceOfMatcher

Instance Method Summary collapse

Instance Method Details

#allow_values_for(attribute, *args, &block) ⇒ Object

Ensures that the attribute can be set to the given values.

Options

  • :allow_nil - when supplied, validates if it allows nil or not.

  • :allow_blank - when supplied, validates if it allows blank or not.

  • :message - value the test expects to find in errors.on(:attribute). Regexp, string or symbol. Default = I18n.translate('activerecord.errors.messages.invalid')

Examples

should_allow_values_for :isbn, "isbn 1 2345 6789 0", "ISBN 1-2345-6789-0"
it { should allow_values_for(:isbn, "isbn 1 2345 6789 0", "ISBN 1-2345-6789-0") }


79
80
81
82
# File 'lib/remarkable/mongo_mapper/matchers/allow_values_for_matcher.rb', line 79

def allow_values_for(attribute, *args, &block)
  options = args.extract_options!
  AllowValuesForMatcher.new(attribute, options.merge!(:in => args), &block).spec(self)
end

#belong_to(*associations, &block) ⇒ Object

Ensures that the many relationship exists. Will also test that the associated table has the required columns.

Options

  • :class_name - the expected associted class name.

  • :polymorphic - if the association should be polymorphic or not. When true it also checks for the association_type column in the subject table.

Examples

should_belong_to :user
should_belong_to :user, :class_name => 'Person'

it { should belong_to(:user) }
it { should belong_to(:user, :class_name => 'Person') }


99
100
101
# File 'lib/remarkable/mongo_mapper/matchers/association_matcher.rb', line 99

def belong_to(*associations, &block)
  AssociationMatcher.new(:belongs_to, *associations, &block).spec(self)
end

#have_key(*args, &block) ⇒ Object Also known as: have_keys

Ensures that a key of the database actually exists.

Examples

should_have_key :name, String

it { should have_key(:name, String) }
it { should have_keys(:name, :phone_number, String) }


31
32
33
# File 'lib/remarkable/mongo_mapper/matchers/have_key_matcher.rb', line 31

def have_key(*args, &block)
  HaveKeyMatcher.new(args.pop, *args, &block).spec(self)
end

#have_many(*associations, &block) ⇒ Object

Ensures that the many relationship exists. Will also test that the associated table has the required columns.

Options

  • :class_name - the expected associted class name.

  • :polymorphic - if the association should be polymorphic or not. When true it also checks for the association_type column in the subject table.

Examples

should_have_many :addresses
should_have_many :users, :class_name => 'Person'

it { should have_many(:addresses) }
it { should have_many(:users, :class_name => 'Person') }


78
79
80
# File 'lib/remarkable/mongo_mapper/matchers/association_matcher.rb', line 78

def have_many(*associations, &block)
  AssociationMatcher.new(:many, *associations, &block).spec(self)
end

#validate_confirmation_of(*attributes, &block) ⇒ Object

Ensures that the model cannot be saved if one of the attributes is not confirmed.

Options

  • :message - value the test expects to find in errors.on(:attribute). Regexp, string or symbol. Default = “doesn’t match confirmation”

Examples

should_validate_confirmation_of :email, :password

it { should validate_confirmation_of(:email, :password) }


38
39
40
# File 'lib/remarkable/mongo_mapper/matchers/validate_confirmation_of_matcher.rb', line 38

def validate_confirmation_of(*attributes, &block)
  ValidateConfirmationOfMatcher.new(*attributes, &block).spec(self)
end

#validate_length_of(*attributes, &block) ⇒ Object

Validates the length of the given attributes. You have also to supply one of the following options: minimum, maximum, is or within.

Note: this method is also aliased as validate_size_of.

Options

  • :minimum - The minimum size of the attribute.

  • :maximum - The maximum size of the attribute.

  • :is - The exact size of the attribute.

  • :within - A range specifying the minimum and maximum size of the attribute.

  • :allow_nil - when supplied, validates if it allows nil or not.

  • :allow_blank - when supplied, validates if it allows blank or not.

  • :message - value the test expects to find in errors.on(:attribute). Regexp, string or symbol. Default = “is invalid”</tt>

Examples

it { should validate_length_of(:password).within(6..20) }
it { should validate_length_of(:password).maximum(20) }
it { should validate_length_of(:password).minimum(6) }
it { should validate_length_of(:age).is(18) }

should_validate_length_of :password, :within => 6..20
should_validate_length_of :password, :maximum => 20
should_validate_length_of :password, :minimum => 6
should_validate_length_of :age, :is => 18

should_validate_length_of :password do |m|
  m.minimum 6
  m.maximum 20
end


100
101
102
# File 'lib/remarkable/mongo_mapper/matchers/validate_length_of_matcher.rb', line 100

def validate_length_of(*attributes, &block)
  ValidateLengthOfMatcher.new(*attributes, &block).spec(self)
end

#validate_presence_of(*args, &block) ⇒ Object

Ensures that the model cannot be saved if one of the attributes listed is not present.

Options

  • :message - value the test expects to find in errors.on(:attribute). Regexp, string or symbol. Default = “can’t be empty”

Examples

should_validate_presence_of :name, :phone_number
it { should validate_presence_of(:name, :phone_number) }


31
32
33
# File 'lib/remarkable/mongo_mapper/matchers/validate_presence_of_matcher.rb', line 31

def validate_presence_of(*args, &block)
  ValidatePresenceOfMatcher.new(*args, &block).spec(self)
end