Module | Shoulda::ActiveRecord::Macros |
In: |
lib/shoulda/active_record/macros.rb
|
These helpers will test most of the validations and associations for your ActiveRecord models.
class UserTest < Test::Unit::TestCase should_validate_presence_of :name, :phone_number should_not_allow_values_for :phone_number, "abcd", "1234" should_allow_values_for :phone_number, "(123) 456-7890" should_not_allow_mass_assignment_of :password should_have_one :profile should_have_many :dogs should_have_many :messes, :through => :dogs should_belong_to :lover end
For all of these helpers, the last parameter may be a hash of options.
Ensures that the attribute can be set on mass update.
should_allow_mass_assignment_of :first_name, :last_name
Ensures that the attribute can be set to the given values.
Example:
should_allow_values_for :isbn, "isbn 1 2345 6789 0", "ISBN 1-2345-6789-0"
Ensures that the length of the attribute is at least a certain length
Options:
Example:
should_ensure_length_at_least :name, 3
Ensures that the length of the attribute is in the given range
Options:
Example:
should_ensure_length_in_range :password, (6..20)
Ensures that the length of the attribute is exactly a certain length
Options:
Example:
should_ensure_length_is :ssn, 9
Ensure that the attribute is in the range specified
Options:
Example:
should_ensure_value_in_range :age, (0..100)
Ensures that the has_and_belongs_to_many relationship exists, and that the join table is in place.
should_have_and_belong_to_many :posts, :cars
Ensure that the given class methods are defined on the model.
should_have_class_methods :find, :destroy
Ensure that the given columns are defined on the models backing SQL table. Also aliased to should_have_db_column for readability. Takes the same options available in migrations: :type, :precision, :limit, :default, :null, and :scale
Examples:
should_have_db_columns :id, :email, :name, :created_at should_have_db_column :email, :type => "string", :limit => 255 should_have_db_column :salary, :decimal, :precision => 15, :scale => 2 should_have_db_column :admin, :default => false, :null => false
Ensures that there are DB indices on the given columns or tuples of columns. Also aliased to should_have_db_index for readability
Options:
Examples:
should_have_db_indices :email, :name, [:commentable_type, :commentable_id] should_have_db_index :age should_have_db_index :ssn, :unique => true
Ensure that the given instance methods are defined on the model.
should_have_instance_methods :email, :name, :name=
Ensures that the has_many relationship exists. Will also test that the associated table has the required columns. Works with polymorphic associations.
Options:
Example:
should_have_many :friends should_have_many :enemies, :through => :friends should_have_many :enemies, :dependent => :destroy
Deprecated.
Ensures that the model has a method named scope_name that returns a NamedScope object with the proxy options set to the options you supply. scope_name can be either a symbol, or a method call which will be evaled against the model. The eval‘d method call has access to all the same instance variables that a should statement would.
Options: Any of the options that the named scope would pass on to find.
Example:
should_have_named_scope :visible, :conditions => {:visible => true}
Passes for
named_scope :visible, :conditions => {:visible => true}
Or for
def self.visible scoped(:conditions => {:visible => true}) end
You can test lambdas or methods that return ActiveRecord#scoped calls:
should_have_named_scope 'recent(5)', :limit => 5 should_have_named_scope 'recent(1)', :limit => 1
Passes for
named_scope :recent, lambda {|c| {:limit => c}}
Or for
def self.recent(c) scoped(:limit => c) end
Ensure that the has_one relationship exists. Will also test that the associated table has the required columns. Works with polymorphic associations.
Options:
Example:
should_have_one :god # unless hindu
Ensures that the attribute cannot be changed once the record has been created.
should_have_readonly_attributes :password, :admin_flag
Ensures that the attribute cannot be set on mass update.
should_not_allow_mass_assignment_of :password, :admin_flag
Ensures that the attribute cannot be set to the given values
Options:
Example:
should_not_allow_values_for :isbn, "bad 1", "bad 2"
Ensures that the model cannot be saved if one of the attributes listed is not accepted.
Options:
Example:
should_validate_acceptance_of :eula
Ensure that the attribute is numeric
Options:
Example:
should_validate_numericality_of :age
Ensures that the model cannot be saved if one of the attributes listed is not present.
Options:
Example:
should_validate_presence_of :name, :phone_number
Examples:
should_validate_uniqueness_of :keyword, :username should_validate_uniqueness_of :name, :message => "O NOES! SOMEONE STOELED YER NAME!" should_validate_uniqueness_of :email, :scoped_to => :name should_validate_uniqueness_of :address, :scoped_to => [:first_name, :last_name] should_validate_uniqueness_of :email, :case_sensitive => false