module Shoulda::Matchers::ActiveRecord
def accept_nested_attributes_for(name)
update_only(true) }
it { should accept_nested_attributes_for(:friends).
limit(4) }
allow_destroy(true).
it { should accept_nested_attributes_for(:friends).
it { should accept_nested_attributes_for(:friends) }
Example:
* update_only - Only allow updates
* limit - Max number of nested attributes
* allow_destroy - Whether or not to allow destroy
Options:
association.
Ensures that the model can accept nested attributes for the specified
def accept_nested_attributes_for(name) AcceptNestedAttributesForMatcher.new(name) end
def belong_to(name)
it { should belong_to(:parent).class_name("ModelClassName") }
it { should belong_to(:parent).validate }
it { should belong_to(:parent).touch }
it { should belong_to(:parent) }
Example:
option.
* touch - tests that the association makes use of the touch
option.
* validate - tests that the association makes use of the validate
* class_name - tests that the association resolves to class_name.
Options:
Ensure that the belongs_to relationship exists.
:nodoc:
def belong_to(name) AssociationMatcher.new(:belongs_to, name) end
def have_and_belong_to_many(name)
it { should have_and_belong_to_many(:posts).class_name("Post") }
it { should have_and_belong_to_many(:posts).validate }
it { should have_and_belong_to_many(:posts) }
Example:
option.
* validate - tests that the association makes use of the validate
* class_name - tests that the association resolves to class_name.
Options:
the join table is in place.
Ensures that the has_and_belongs_to_many relationship exists, and that
def have_and_belong_to_many(name) AssociationMatcher.new(:has_and_belongs_to_many, name) end
def have_db_column(column)
with_options(precision: 10, scale: 2) }
of_type(:decimal).
it { should have_db_column(:salary).
it { should_not have_db_column(:admin).of_type(:boolean) }
Examples:
(:default, :null, :limit, :precision, :scale)
* with_options - same options available in migrations
* of_type - db column type (:integer, :string, etc.)
Options:
Ensures the database column exists.
def have_db_column(column) HaveDbColumnMatcher.new(column) end
def have_db_index(columns)
it { should have_db_index(:ssn).unique(true) }
it { should have_db_index([:commentable_type, :commentable_id]) }
it { should have_db_index(:age) }
Examples:
constraint.
constraint. Use false to explicitly test for a non-unique
constraint. Use true to explicitly test for a unique
* unique - whether or not the index has a unique
Options:
columns.
Ensures that there are DB indices on the given columns or tuples of
def have_db_index(columns) HaveDbIndexMatcher.new(columns) end
def have_many(name)
it { should have_many(:friends).class_name("Friend") }
it { should have_many(:friends).validate }
it { should have_many(:friends).autosave }
it { should have_many(:enemies).dependent(:destroy) }
it { should have_many(:enemies).through(:friends) }
it { should have_many(:friends) }
Example:
option.
* validate - tests that the association makes use of the validate
autosave option.
* autosave - tests that the association makes use of the
* class_name - tests that the association resoves to class_name.
dependent option.
* dependent - tests that the association makes use of the
* through - association name for has_many :through
Options:
associations.
associated table has the required columns. Works with polymorphic
Ensures that the has_many relationship exists. Will also test that the
def have_many(name) AssociationMatcher.new(:has_many, name) end
def have_one(name)
it { should have_one(:god).class_name("JHVH1") }
it { should have_one(:god).validate }
it { should have_one(:god).autosave }
it { should have_one(:god).dependent }
it { should have_one(:god) } # unless hindu
Example:
option.
* validate - tests that the association makes use of the validate
autosave option.
* autosave - tests that the association makes use of the
* class_name - tests that the association resolves to class_name.
dependent option.
* dependent - tests that the association makes use of the
Options:
associations.
associated table has the required columns. Works with polymorphic
Ensure that the has_one relationship exists. Will also test that the
def have_one(name) AssociationMatcher.new(:has_one, name) end
def have_readonly_attribute(value)
it { should have_readonly_attribute(:password) }
created.
Ensures that the attribute cannot be changed once the record has been
def have_readonly_attribute(value) HaveReadonlyAttributeMatcher.new(value) end
def serialize(name)
it { should serialize(:details).as_instance_of(ExampleSerializer) }
it { should serialize(:details).as(Hash) }
it { should serialize(:details) }
Example:
* :as - tests that the serialized attribute makes use of the class_name option.
Options:
Ensure that the field becomes serialized.
:nodoc:
def serialize(name) SerializeMatcher.new(name) end