Module: Shoulda::Matchers::ActiveRecord
- Included in:
- Test::Unit::TestCase, Test::Unit::TestCase
- Defined in:
- lib/shoulda/matchers/active_record.rb,
lib/shoulda/matchers/active_record/serialize_matcher.rb,
lib/shoulda/matchers/active_record/association_matcher.rb,
lib/shoulda/matchers/active_record/have_db_index_matcher.rb,
lib/shoulda/matchers/active_record/have_db_column_matcher.rb,
lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb,
lib/shoulda/matchers/active_record/accept_nested_attributes_for_matcher.rb
Overview
:nodoc:
Defined Under Namespace
Classes: AcceptNestedAttributesForMatcher, AssociationMatcher, HaveDbColumnMatcher, HaveDbIndexMatcher, HaveReadonlyAttributeMatcher, SerializeMatcher
Instance Method Summary (collapse)
-
- (Object) accept_nested_attributes_for(name)
Ensures that the model can accept nested attributes for the specified association.
-
- (Object) belong_to(name)
Ensure that the belongs_to relationship exists.
-
- (Object) have_and_belong_to_many(name)
Ensures that the has_and_belongs_to_many relationship exists, and that the join table is in place.
-
- (Object) have_db_column(column)
Ensures the database column exists.
-
- (Object) have_db_index(columns)
Ensures that there are DB indices on the given columns or tuples of columns.
-
- (Object) have_many(name)
Ensures that the has_many relationship exists.
-
- (Object) have_one(name)
Ensure that the has_one relationship exists.
-
- (Object) have_readonly_attribute(value)
Ensures that the attribute cannot be changed once the record has been created.
-
- (Object) serialize(name)
Ensure that the field becomes serialized.
Instance Method Details
- (Object) accept_nested_attributes_for(name)
Ensures that the model can accept nested attributes for the specified association.
Options:
-
allow_destroy - Whether or not to allow destroy
-
limit - Max number of nested attributes
-
update_only - Only allow updates
Example:
it { should accept_nested_attributes_for(:friends) }
it { should accept_nested_attributes_for(:friends).
allow_destroy(true).
limit(4) }
it { should accept_nested_attributes_for(:friends).
update_only(true) }
20 21 22 |
# File 'lib/shoulda/matchers/active_record/accept_nested_attributes_for_matcher.rb', line 20 def accept_nested_attributes_for(name) AcceptNestedAttributesForMatcher.new(name) end |
- (Object) belong_to(name)
Ensure that the belongs_to relationship exists.
Options:
-
:class_name - tests that the association resolves to class_name.
-
:validate - tests that the association makes use of the validate
option.
-
:touch - tests that the association makes use of the touch
option.
Example:
it { should belong_to(:parent) }
16 17 18 |
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 16 def belong_to(name) AssociationMatcher.new(:belongs_to, name) end |
- (Object) have_and_belong_to_many(name)
Ensures that the has_and_belongs_to_many relationship exists, and that the join table is in place.
Options:
-
:class_name - tests that the association resolves to class_name.
-
:validate - tests that the association makes use of the validate
option.
Example:
it { should have_and_belong_to_many(:posts) }
70 71 72 |
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 70 def have_and_belong_to_many(name) AssociationMatcher.new(:has_and_belongs_to_many, name) end |
- (Object) have_db_column(column)
Ensures the database column exists.
Options:
-
of_type - db column type (:integer, :string, etc.)
-
with_options - same options available in migrations (:default, :null, :limit, :precision, :scale)
Examples:
it { should_not have_db_column(:admin).of_type(:boolean) }
it { should have_db_column(:salary).
of_type(:decimal).
(:precision => 10, :scale => 2) }
18 19 20 |
# File 'lib/shoulda/matchers/active_record/have_db_column_matcher.rb', line 18 def have_db_column(column) HaveDbColumnMatcher.new(column) end |
- (Object) have_db_index(columns)
Ensures that there are DB indices on the given columns or tuples of columns.
Options:
-
unique - whether or not the index has a unique constraint. Use true to explicitly test for a unique constraint. Use false to explicitly test for a non-unique constraint.
Examples:
it { should have_db_index(:age) }
it { should have_db_index([:commentable_type, :commentable_id]) }
it { should have_db_index(:ssn).unique(true) }
20 21 22 |
# File 'lib/shoulda/matchers/active_record/have_db_index_matcher.rb', line 20 def have_db_index(columns) HaveDbIndexMatcher.new(columns) end |
- (Object) have_many(name)
Ensures that the has_many relationship exists. Will also test that the associated table has the required columns. Works with polymorphic associations.
Options:
-
through - association name for has_many :through
-
dependent - tests that the association makes use of the dependent option.
-
:class_name - tests that the association resoves to class_name.
-
:validate - tests that the association makes use of the validate
option.
Example:
it { should have_many(:friends) }
it { should have_many(:enemies).through(:friends) }
it { should have_many(:enemies).dependent(:destroy) }
37 38 39 |
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 37 def have_many(name) AssociationMatcher.new(:has_many, name) end |
- (Object) have_one(name)
Ensure that the has_one relationship exists. Will also test that the associated table has the required columns. Works with polymorphic associations.
Options:
-
:dependent - tests that the association makes use of the dependent option.
-
:class_name - tests that the association resolves to class_name.
-
:validate - tests that the association makes use of the validate
option.
Example:
it { should have_one(:god) } # unless hindu
55 56 57 |
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 55 def have_one(name) AssociationMatcher.new(:has_one, name) end |
- (Object) have_readonly_attribute(value)
Ensures that the attribute cannot be changed once the record has been created.
it { should have_readonly_attribute(:password) }
10 11 12 |
# File 'lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb', line 10 def have_readonly_attribute(value) HaveReadonlyAttributeMatcher.new(value) end |
- (Object) serialize(name)
Ensure that the field becomes serialized.
Options:
-
:as - tests that the serialized attribute makes use of the class_name option.
Example:
it { should serialize(:details) }
it { should serialize(:details).as(Hash) }
it { should serialize(:details).as_instance_of(ExampleSerializer) }
14 15 16 |
# File 'lib/shoulda/matchers/active_record/serialize_matcher.rb', line 14 def serialize(name) SerializeMatcher.new(name) end |