Module: Remarkable::MongoMapper::Matchers

Defined in:
lib/remarkable_mongomapper/matchers/have_key_matcher.rb,
lib/remarkable_mongomapper/matchers/association_matcher.rb,
lib/remarkable_mongomapper/matchers/allow_values_for_matcher.rb,
lib/remarkable_mongomapper/matchers/validate_presence_of_matcher.rb

Defined Under Namespace

Classes: AllowValuesForMatcher, AssociationMatcher, HaveKeyMatcher, 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_mongomapper/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_mongomapper/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_mongomapper/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_mongomapper/matchers/association_matcher.rb', line 78

def have_many(*associations, &block)
  AssociationMatcher.new(:many, *associations, &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_mongomapper/matchers/validate_presence_of_matcher.rb', line 31

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