Module: Validity::TestUnit::Record
- Extended by:
- Test::Unit::Assertions
- Defined in:
- lib/validity/frameworks/test_unit/record.rb
Class Method Summary collapse
-
.belongs_to(record, field, target) ⇒ Object
Asserts a record has a
belongs_toassociation as indicated by the providedfieldand that the record equals thetargetrecord, if provided. -
.delegates(record, delegated, delegated_to) ⇒ Object
Asserts that a record responds to the
delegatedmethod and that the returned object is equal to the object referenced bydelegated_to. -
.field_presence(record, field) ⇒ Object
Asserts that the given
fieldmust be present for a record to be valid. -
.field_uniqueness(record, field) ⇒ Object
Asserts that the given
fieldmust be unique for a record to be valid. -
.has_many(record, field, targets) ⇒ Object
Asserts a record has a
has_manyassociation as indicated by the providedfieldand that the many records equal thetargetsrecords, if provided.
Class Method Details
.belongs_to(record, field, target) ⇒ Object
Asserts a record has a belongs_to association as indicated by the provided field and that the record equals the target record, if provided.
-
Args:
-
recordthe record to validate -
fieldthe fields to check for has_many association -
targetsthe target associated records
-
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/validity/frameworks/test_unit/record.rb', line 12 def self.belongs_to(record, field, target) clazz = record.class assert_respond_to record, field, "#{clazz} cannot find associated #{field}" one = record.send(field) assert_not_nil one, "#{clazz} does not have associated #{field}" if target assert_equal target, one, "#{field.to_s.capitalize} associated with this #{clazz.to_s.downcase} is not the target #{field}" end end |
.delegates(record, delegated, delegated_to) ⇒ Object
Asserts that a record responds to the delegated method and that the returned object is equal to the object referenced by delegated_to.
-
Args:
-
recordthe record to validate -
fieldthe fields to check for presence
-
29 30 31 32 33 |
# File 'lib/validity/frameworks/test_unit/record.rb', line 29 def self.delegates(record, delegated, delegated_to) clazz = record.class assert_respond_to record, delegated, "#{clazz} does not respond to #{delegated}" assert_equal delegated_to.send(delegated), record.send(delegated), "Delegated objects do not match" end |
.field_presence(record, field) ⇒ Object
Asserts that the given field must be present for a record to be valid.
-
Args:
-
recordthe record to validate -
fieldthe fields to check for presence
-
40 41 42 43 44 45 46 47 |
# File 'lib/validity/frameworks/test_unit/record.rb', line 40 def self.field_presence(record, field) record.send("#{field}=", nil) clazz = record.class assert !record.valid?, "#{clazz} is considered valid with nil #{field}" assert !record.save, "#{clazz} saved without #{field} field" assert record.errors[field].any?, "#{clazz} does not have an error on #{field}" end |
.field_uniqueness(record, field) ⇒ Object
Asserts that the given field must be unique for a record to be valid.
-
Args:
-
recordthe record to validate -
fieldthe fields to check for uniqueness
-
54 55 56 57 58 59 60 61 |
# File 'lib/validity/frameworks/test_unit/record.rb', line 54 def self.field_uniqueness(record, field) dup = record.dup clazz = dup.class assert !dup.valid?, "#{clazz} is considered valid with duplicate #{field}" assert !dup.save, "#{clazz} saved with a duplicate #{field}" assert dup.errors[field].any?, "#{clazz} does not have an error on #{field}" end |
.has_many(record, field, targets) ⇒ Object
Asserts a record has a has_many association as indicated by the provided field and that the many records equal the targets records, if provided.
-
Args:
-
recordthe record to validate -
fieldthe fields to check for has_many association -
targetsthe target associated records
-
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/validity/frameworks/test_unit/record.rb', line 70 def self.has_many(record, field, targets) clazz = record.class assert_respond_to record, field, "#{clazz} cannot find associated #{field}" many = record.send(field) assert !(many.nil? || many.empty?), "#{clazz} does not have associated #{field}" if targets assert_equal targets.size, many.size, "#{clazz} does not have #{targets.size} associated #{field}" end end |