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.
Deprecated: use ActiveRecord::Matchers#allow_mass_assignment_of instead.
Ensures that the attribute can be set on mass update.
should_allow_mass_assignment_of :first_name, :last_name
# File lib/shoulda/active_record/macros.rb, line 85 85: def should_allow_mass_assignment_of(*attributes) 86: ::ActiveSupport::Deprecation.warn("use: should allow_mass_assignment_of") 87: get_options!(attributes) 88: 89: attributes.each do |attribute| 90: should allow_mass_assignment_of(attribute) 91: end 92: end
Deprecated: use ActiveRecord::Matchers#allow_value instead.
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"
# File lib/shoulda/active_record/macros.rb, line 151 151: def should_allow_values_for(attribute, *good_values) 152: ::ActiveSupport::Deprecation.warn("use: should allow_value") 153: get_options!(good_values) 154: good_values.each do |value| 155: should allow_value(value).for(attribute) 156: end 157: end
Deprecated: use ActiveRecord::Matchers#belong_to instead.
Ensure that the belongs_to relationship exists.
should_belong_to :parent
# File lib/shoulda/active_record/macros.rb, line 334 334: def should_belong_to(*associations) 335: ::ActiveSupport::Deprecation.warn("use: should belong_to") 336: dependent = get_options!(associations, :dependent) 337: associations.each do |association| 338: should belong_to(association).dependent(dependent) 339: end 340: end
Deprecated: use ActiveRecord::Matchers#ensure_length_of instead.
Ensures that the length of the attribute is at least a certain length
Options:
:short_message - value the test expects to find in errors.on(:attribute). Regexp or string. Default = I18n.translate('activerecord.errors.messages.too_short') % min_length
Example:
should_ensure_length_at_least :name, 3
# File lib/shoulda/active_record/macros.rb, line 195 195: def should_ensure_length_at_least(attribute, min_length, opts = {}) 196: ::ActiveSupport::Deprecation.warn("use: should ensure_length_of.is_at_least") 197: short_message = get_options!([opts], :short_message) 198: 199: should ensure_length_of(attribute). 200: is_at_least(min_length). 201: with_short_message(short_message) 202: end
Deprecated: use ActiveRecord::Matchers#ensure_length_of instead.
Ensures that the length of the attribute is in the given range
Options:
:short_message - value the test expects to find in errors.on(:attribute). Regexp or string. Default = I18n.translate('activerecord.errors.messages.too_short') % range.first
:long_message - value the test expects to find in errors.on(:attribute). Regexp or string. Default = I18n.translate('activerecord.errors.messages.too_long') % range.last
Example:
should_ensure_length_in_range :password, (6..20)
# File lib/shoulda/active_record/macros.rb, line 172 172: def should_ensure_length_in_range(attribute, range, opts = {}) 173: ::ActiveSupport::Deprecation.warn("use: should ensure_length_of.is_at_least.is_at_most") 174: short_message, long_message = get_options!([opts], 175: :short_message, 176: :long_message) 177: should ensure_length_of(attribute). 178: is_at_least(range.first). 179: with_short_message(short_message). 180: is_at_most(range.last). 181: with_long_message(long_message) 182: end
Deprecated: use ActiveRecord::Matchers#ensure_length_of instead.
Ensures that the length of the attribute is exactly a certain length
Options:
:message - value the test expects to find in errors.on(:attribute). Regexp or string. Default = I18n.translate('activerecord.errors.messages.wrong_length') % length
Example:
should_ensure_length_is :ssn, 9
# File lib/shoulda/active_record/macros.rb, line 215 215: def should_ensure_length_is(attribute, length, opts = {}) 216: ::ActiveSupport::Deprecation.warn("use: should ensure_length_of.is_equal_to") 217: message = get_options!([opts], :message) 218: should ensure_length_of(attribute). 219: is_equal_to(length). 220: with_message(message) 221: end
Deprecated: use ActiveRecord::Matchers#ensure_inclusion_of instead.
Ensure that the attribute is in the range specified
Options:
:low_message - value the test expects to find in errors.on(:attribute). Regexp or string. Default = I18n.translate('activerecord.errors.messages.inclusion')
:high_message - value the test expects to find in errors.on(:attribute). Regexp or string. Default = I18n.translate('activerecord.errors.messages.inclusion')
Example:
should_ensure_value_in_range :age, (0..100)
# File lib/shoulda/active_record/macros.rb, line 236 236: def should_ensure_value_in_range(attribute, range, opts = {}) 237: ::ActiveSupport::Deprecation.warn("use: should ensure_inclusion_of.in_range") 238: message, low_message, high_message = get_options!([opts], 239: :message, 240: :low_message, 241: :high_message) 242: should ensure_inclusion_of(attribute). 243: in_range(range). 244: with_message(message). 245: with_low_message(low_message). 246: with_high_message(high_message) 247: end
Deprecated: use ActiveRecord::Matchers#have_and_belong_to_many instead.
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
# File lib/shoulda/active_record/macros.rb, line 319 319: def should_have_and_belong_to_many(*associations) 320: ::ActiveSupport::Deprecation.warn("use: should have_and_belong_to_many") 321: get_options!(associations) 322: 323: associations.each do |association| 324: should have_and_belong_to_many(association) 325: end 326: end
Deprecated.
Ensure that the given class methods are defined on the model.
should_have_class_methods :find, :destroy
# File lib/shoulda/active_record/macros.rb, line 348 348: def should_have_class_methods(*methods) 349: ::ActiveSupport::Deprecation.warn 350: get_options!(methods) 351: klass = described_type 352: methods.each do |method| 353: should "respond to class method ##{method}" do 354: assert_respond_to klass, method, "#{klass.name} does not have class method #{method}" 355: end 356: end 357: end
Deprecated: use ActiveRecord::Matchers#have_db_column instead.
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
# File lib/shoulda/active_record/macros.rb, line 391 391: def should_have_db_columns(*columns) 392: ::ActiveSupport::Deprecation.warn("use: should have_db_column") 393: column_type, precision, limit, default, null, scale, sql_type = 394: get_options!(columns, :type, :precision, :limit, 395: :default, :null, :scale, :sql_type) 396: columns.each do |name| 397: should have_db_column(name). 398: of_type(column_type). 399: with_options(:precision => precision, :limit => limit, 400: :default => default, :null => null, 401: :scale => scale, :sql_type => sql_type) 402: end 403: end
Deprecated: use ActiveRecord::Matchers#have_db_index instead.
Ensures that there are DB indices on the given columns or tuples of columns. Also aliased to should_have_db_index for readability
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. Use nil if you don’t care whether the index is unique or not. Default = nil
Examples:
should_have_db_indices :email, :name, [:commentable_type, :commentable_id] should_have_db_index :age should_have_db_index :ssn, :unique => true
# File lib/shoulda/active_record/macros.rb, line 425 425: def should_have_db_indices(*columns) 426: ::ActiveSupport::Deprecation.warn("use: should have_db_index") 427: unique = get_options!(columns, :unique) 428: 429: columns.each do |column| 430: should have_db_index(column).unique(unique) 431: end 432: end
Deprecated.
Ensure that the given instance methods are defined on the model.
should_have_instance_methods :email, :name, :name=
# File lib/shoulda/active_record/macros.rb, line 365 365: def should_have_instance_methods(*methods) 366: ::ActiveSupport::Deprecation.warn 367: get_options!(methods) 368: klass = described_type 369: methods.each do |method| 370: should "respond to instance method ##{method}" do 371: assert_respond_to klass.new, method, "#{klass.name} does not have instance method #{method}" 372: end 373: end 374: end
Deprecated: use ActiveRecord::Matchers#have_many instead.
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.
Example:
should_have_many :friends should_have_many :enemies, :through => :friends should_have_many :enemies, :dependent => :destroy
# File lib/shoulda/active_record/macros.rb, line 284 284: def should_have_many(*associations) 285: ::ActiveSupport::Deprecation.warn("use: should have_many") 286: through, dependent = get_options!(associations, :through, :dependent) 287: associations.each do |association| 288: should have_many(association).through(through).dependent(dependent) 289: end 290: end
Deprecated: use ActiveRecord::Matchers#have_one instead.
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.
Example:
should_have_one :god # unless hindu
# File lib/shoulda/active_record/macros.rb, line 304 304: def should_have_one(*associations) 305: ::ActiveSupport::Deprecation.warn("use: should have_one") 306: dependent, through = get_options!(associations, :dependent, :through) 307: associations.each do |association| 308: should have_one(association).dependent(dependent).through(through) 309: end 310: end
Deprecated: use ActiveRecord::Matchers#have_readonly_attribute instead.
Ensures that the attribute cannot be changed once the record has been created.
should_have_readonly_attributes :password, :admin_flag
# File lib/shoulda/active_record/macros.rb, line 115 115: def should_have_readonly_attributes(*attributes) 116: ::ActiveSupport::Deprecation.warn("use: should have_readonly_attribute") 117: get_options!(attributes) 118: 119: attributes.each do |attribute| 120: should have_readonly_attribute(attribute) 121: end 122: end
Deprecated: use ActiveRecord::Matchers#allow_mass_assignment_of instead.
Ensures that the attribute cannot be set on mass update.
should_not_allow_mass_assignment_of :password, :admin_flag
# File lib/shoulda/active_record/macros.rb, line 100 100: def should_not_allow_mass_assignment_of(*attributes) 101: ::ActiveSupport::Deprecation.warn("use: should_not allow_mass_assignment_of") 102: get_options!(attributes) 103: 104: attributes.each do |attribute| 105: should_not allow_mass_assignment_of(attribute) 106: end 107: end
Deprecated: use ActiveRecord::Matchers#allow_value instead.
Ensures that the attribute cannot be set to the given values
Options:
:message - value the test expects to find in errors.on(:attribute). Regexp or string. If omitted, the test will pass if there is ANY error in errors.on(:attribute).
Example:
should_not_allow_values_for :isbn, "bad 1", "bad 2"
# File lib/shoulda/active_record/macros.rb, line 136 136: def should_not_allow_values_for(attribute, *bad_values) 137: ::ActiveSupport::Deprecation.warn("use: should_not allow_value") 138: message = get_options!(bad_values, :message) 139: bad_values.each do |value| 140: should_not allow_value(value).for(attribute).with_message(message) 141: end 142: end
Deprecated: use ActiveRecord::Matchers#validate_acceptance_of instead.
Ensures that the model cannot be saved if one of the attributes listed is not accepted.
Options:
:message - value the test expects to find in errors.on(:attribute). Regexp or string. Default = I18n.translate('activerecord.errors.messages.accepted')
Example:
should_validate_acceptance_of :eula
# File lib/shoulda/active_record/macros.rb, line 447 447: def should_validate_acceptance_of(*attributes) 448: ::ActiveSupport::Deprecation.warn("use: should validate_acceptance_of") 449: message = get_options!(attributes, :message) 450: 451: attributes.each do |attribute| 452: should validate_acceptance_of(attribute).with_message(message) 453: end 454: end
Deprecated: use ActiveRecord::Matchers#validate_numericality_of instead.
Ensure that the attribute is numeric
Options:
:message - value the test expects to find in errors.on(:attribute). Regexp or string. Default = I18n.translate('activerecord.errors.messages.not_a_number')
Example:
should_validate_numericality_of :age
# File lib/shoulda/active_record/macros.rb, line 260 260: def should_validate_numericality_of(*attributes) 261: ::ActiveSupport::Deprecation.warn("use: should validate_numericality_of") 262: message = get_options!(attributes, :message) 263: attributes.each do |attribute| 264: should validate_numericality_of(attribute). 265: with_message(message) 266: end 267: end
Deprecated: use ActiveRecord::Matchers#validate_presence_of instead.
Ensures that the model cannot be saved if one of the attributes listed is not present.
Options:
:message - value the test expects to find in errors.on(:attribute). Regexp or string. Default = I18n.translate('activerecord.errors.messages.blank')
Example:
should_validate_presence_of :name, :phone_number
# File lib/shoulda/active_record/macros.rb, line 37 37: def should_validate_presence_of(*attributes) 38: ::ActiveSupport::Deprecation.warn("use: should validate_presence_of") 39: message = get_options!(attributes, :message) 40: 41: attributes.each do |attribute| 42: should validate_presence_of(attribute).with_message(message) 43: end 44: end
Deprecated: use ActiveRecord::Matchers#validate_uniqueness_of instead.
Ensures that the model cannot be saved if one of the attributes listed is not unique. Requires an existing record
Options:
:message - value the test expects to find in errors.on(:attribute). Regexp or string. Default = I18n.translate('activerecord.errors.messages.taken')
:scoped_to - field(s) to scope the uniqueness to.
:case_sensitive - whether or not uniqueness is defined by an exact match. Ignored by non-text attributes. Default = true
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
# File lib/shoulda/active_record/macros.rb, line 65 65: def should_validate_uniqueness_of(*attributes) 66: ::ActiveSupport::Deprecation.warn("use: should validate_uniqueness_of") 67: message, scope, case_sensitive = get_options!(attributes, :message, :scoped_to, :case_sensitive) 68: scope = [*scope].compact 69: case_sensitive = true if case_sensitive.nil? 70: 71: attributes.each do |attribute| 72: matcher = validate_uniqueness_of(attribute). 73: with_message(message).scoped_to(scope) 74: matcher = matcher.case_insensitive unless case_sensitive 75: should matcher 76: end 77: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.