Module: Shoulda::Matchers::ActiveRecord

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/association_matchers/order_matcher.rb,
lib/shoulda/matchers/active_record/association_matchers/source_matcher.rb,
lib/shoulda/matchers/active_record/accept_nested_attributes_for_matcher.rb,
lib/shoulda/matchers/active_record/association_matchers/model_reflector.rb,
lib/shoulda/matchers/active_record/association_matchers/option_verifier.rb,
lib/shoulda/matchers/active_record/association_matchers/through_matcher.rb,
lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb,
lib/shoulda/matchers/active_record/association_matchers/dependent_matcher.rb,
lib/shoulda/matchers/active_record/association_matchers/inverse_of_matcher.rb,
lib/shoulda/matchers/active_record/association_matchers/counter_cache_matcher.rb

Overview

:nodoc:

Defined Under Namespace

Modules: AssociationMatchers Classes: AcceptNestedAttributesForMatcher, AssociationMatcher, HaveDbColumnMatcher, HaveDbIndexMatcher, HaveReadonlyAttributeMatcher, SerializeMatcher

Instance Method Summary (collapse)

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) }
it { should belong_to(:parent).touch }
it { should belong_to(:parent).validate }
it { should belong_to(:parent).class_name("ModelClassName") }


21
22
23
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 21

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) }
it { should have_and_belong_to_many(:posts).validate }
it { should have_and_belong_to_many(:posts).class_name("Post") }


88
89
90
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 88

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).
              with_options(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.

  • autosave - tests that the association makes use of the autosave option.

  • 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) }
it { should have_many(:friends).autosave }
it { should have_many(:friends).validate }
it { should have_many(:friends).class_name("Friend") }


47
48
49
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 47

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.

  • autosave - tests that the association makes use of the autosave option.

  • validate - tests that the association makes use of the validate

option.

Example:

it { should have_one(:god) } # unless hindu
it { should have_one(:god).dependent }
it { should have_one(:god).autosave }
it { should have_one(:god).validate }
it { should have_one(:god).class_name("JHVH1") }


71
72
73
# File 'lib/shoulda/matchers/active_record/association_matcher.rb', line 71

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