Module: Shoulda::ActiveRecord::Macros
- Included in:
- Test::Unit::TestCase
- Defined in:
- lib/shoulda/active_record/macros.rb
Overview
Macro test helpers for your active record models
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.
Instance Method Summary collapse
-
#should_allow_mass_assignment_of(*attributes) ⇒ Object
Deprecated: use ActiveRecord::Matchers#allow_mass_assignment_of instead.
-
#should_allow_values_for(attribute, *good_values) ⇒ Object
Deprecated: use ActiveRecord::Matchers#allow_value instead.
-
#should_belong_to(*associations) ⇒ Object
Deprecated: use ActiveRecord::Matchers#belong_to instead.
-
#should_ensure_length_at_least(attribute, min_length, opts = {}) ⇒ Object
Deprecated: use ActiveRecord::Matchers#ensure_length_of instead.
-
#should_ensure_length_in_range(attribute, range, opts = {}) ⇒ Object
Deprecated: use ActiveRecord::Matchers#ensure_length_of instead.
-
#should_ensure_length_is(attribute, length, opts = {}) ⇒ Object
Deprecated: use ActiveRecord::Matchers#ensure_length_of instead.
-
#should_ensure_value_in_range(attribute, range, opts = {}) ⇒ Object
Deprecated: use ActiveRecord::Matchers#ensure_inclusion_of instead.
-
#should_have_and_belong_to_many(*associations) ⇒ Object
Deprecated: use ActiveRecord::Matchers#have_and_belong_to_many instead.
-
#should_have_class_methods(*methods) ⇒ Object
Deprecated.
-
#should_have_db_columns(*columns) ⇒ Object
(also: #should_have_db_column)
Deprecated: use ActiveRecord::Matchers#have_db_column instead.
-
#should_have_db_indices(*columns) ⇒ Object
(also: #should_have_db_index)
Deprecated: use ActiveRecord::Matchers#have_db_index instead.
-
#should_have_instance_methods(*methods) ⇒ Object
Deprecated.
-
#should_have_many(*associations) ⇒ Object
Deprecated: use ActiveRecord::Matchers#have_many instead.
-
#should_have_one(*associations) ⇒ Object
Deprecated: use ActiveRecord::Matchers#have_one instead.
-
#should_have_readonly_attributes(*attributes) ⇒ Object
Deprecated: use ActiveRecord::Matchers#have_readonly_attribute instead.
-
#should_not_allow_mass_assignment_of(*attributes) ⇒ Object
Deprecated: use ActiveRecord::Matchers#allow_mass_assignment_of instead.
-
#should_not_allow_values_for(attribute, *bad_values) ⇒ Object
Deprecated: use ActiveRecord::Matchers#allow_value instead.
-
#should_validate_acceptance_of(*attributes) ⇒ Object
Deprecated: use ActiveRecord::Matchers#validate_acceptance_of instead.
-
#should_validate_numericality_of(*attributes) ⇒ Object
Deprecated: use ActiveRecord::Matchers#validate_numericality_of instead.
-
#should_validate_presence_of(*attributes) ⇒ Object
Deprecated: use ActiveRecord::Matchers#validate_presence_of instead.
-
#should_validate_uniqueness_of(*attributes) ⇒ Object
Deprecated: use ActiveRecord::Matchers#validate_uniqueness_of instead.
Methods included from Matchers
#allow_mass_assignment_of, #allow_value, #belong_to, #ensure_inclusion_of, #ensure_length_of, #have_and_belong_to_many, #have_db_column, #have_db_index, #have_many, #have_one, #have_readonly_attribute, #validate_acceptance_of, #validate_format_of, #validate_numericality_of, #validate_presence_of, #validate_uniqueness_of
Methods included from Helpers
Instance Method Details
#should_allow_mass_assignment_of(*attributes) ⇒ Object
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
85 86 87 88 89 90 91 92 |
# File 'lib/shoulda/active_record/macros.rb', line 85 def should_allow_mass_assignment_of(*attributes) ::ActiveSupport::Deprecation.warn("use: should allow_mass_assignment_of") (attributes) attributes.each do |attribute| should allow_mass_assignment_of(attribute) end end |
#should_allow_values_for(attribute, *good_values) ⇒ Object
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"
151 152 153 154 155 156 157 |
# File 'lib/shoulda/active_record/macros.rb', line 151 def should_allow_values_for(attribute, *good_values) ::ActiveSupport::Deprecation.warn("use: should allow_value") (good_values) good_values.each do |value| should allow_value(value).for(attribute) end end |
#should_belong_to(*associations) ⇒ Object
Deprecated: use ActiveRecord::Matchers#belong_to instead.
Ensure that the belongs_to relationship exists.
should_belong_to :parent
334 335 336 337 338 339 340 |
# File 'lib/shoulda/active_record/macros.rb', line 334 def should_belong_to(*associations) ::ActiveSupport::Deprecation.warn("use: should belong_to") dependent = (associations, :dependent) associations.each do |association| should belong_to(association).dependent(dependent) end end |
#should_ensure_length_at_least(attribute, min_length, opts = {}) ⇒ Object
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 inerrors.on(:attribute)
. Regexp or string. Default =I18n.translate('activerecord.errors.messages.too_short') % min_length
Example:
should_ensure_length_at_least :name, 3
195 196 197 198 199 200 201 202 |
# File 'lib/shoulda/active_record/macros.rb', line 195 def should_ensure_length_at_least(attribute, min_length, opts = {}) ::ActiveSupport::Deprecation.warn("use: should ensure_length_of.is_at_least") = ([opts], :short_message) should ensure_length_of(attribute). is_at_least(min_length). () end |
#should_ensure_length_in_range(attribute, range, opts = {}) ⇒ Object
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 inerrors.on(:attribute)
. Regexp or string. Default =I18n.translate('activerecord.errors.messages.too_short') % range.first
-
:long_message
- value the test expects to find inerrors.on(:attribute)
. Regexp or string. Default =I18n.translate('activerecord.errors.messages.too_long') % range.last
Example:
should_ensure_length_in_range :password, (6..20)
172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/shoulda/active_record/macros.rb', line 172 def should_ensure_length_in_range(attribute, range, opts = {}) ::ActiveSupport::Deprecation.warn("use: should ensure_length_of.is_at_least.is_at_most") , = ([opts], :short_message, :long_message) should ensure_length_of(attribute). is_at_least(range.first). (). is_at_most(range.last). () end |
#should_ensure_length_is(attribute, length, opts = {}) ⇒ Object
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 inerrors.on(:attribute)
. Regexp or string. Default =I18n.translate('activerecord.errors.messages.wrong_length') % length
Example:
should_ensure_length_is :ssn, 9
215 216 217 218 219 220 221 |
# File 'lib/shoulda/active_record/macros.rb', line 215 def should_ensure_length_is(attribute, length, opts = {}) ::ActiveSupport::Deprecation.warn("use: should ensure_length_of.is_equal_to") = ([opts], :message) should ensure_length_of(attribute). is_equal_to(length). () end |
#should_ensure_value_in_range(attribute, range, opts = {}) ⇒ Object
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 inerrors.on(:attribute)
. Regexp or string. Default =I18n.translate('activerecord.errors.messages.inclusion')
-
:high_message
- value the test expects to find inerrors.on(:attribute)
. Regexp or string. Default =I18n.translate('activerecord.errors.messages.inclusion')
Example:
should_ensure_value_in_range :age, (0..100)
236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/shoulda/active_record/macros.rb', line 236 def should_ensure_value_in_range(attribute, range, opts = {}) ::ActiveSupport::Deprecation.warn("use: should ensure_inclusion_of.in_range") , , = ([opts], :message, :low_message, :high_message) should ensure_inclusion_of(attribute). in_range(range). (). (). () end |
#should_have_and_belong_to_many(*associations) ⇒ Object
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
319 320 321 322 323 324 325 326 |
# File 'lib/shoulda/active_record/macros.rb', line 319 def should_have_and_belong_to_many(*associations) ::ActiveSupport::Deprecation.warn("use: should have_and_belong_to_many") (associations) associations.each do |association| should have_and_belong_to_many(association) end end |
#should_have_class_methods(*methods) ⇒ Object
Deprecated.
Ensure that the given class methods are defined on the model.
should_have_class_methods :find, :destroy
348 349 350 351 352 353 354 355 356 357 |
# File 'lib/shoulda/active_record/macros.rb', line 348 def should_have_class_methods(*methods) ::ActiveSupport::Deprecation.warn (methods) klass = described_type methods.each do |method| should "respond to class method ##{method}" do assert_respond_to klass, method, "#{klass.name} does not have class method #{method}" end end end |
#should_have_db_columns(*columns) ⇒ Object Also known as: should_have_db_column
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
391 392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'lib/shoulda/active_record/macros.rb', line 391 def should_have_db_columns(*columns) ::ActiveSupport::Deprecation.warn("use: should have_db_column") column_type, precision, limit, default, null, scale, sql_type = (columns, :type, :precision, :limit, :default, :null, :scale, :sql_type) columns.each do |name| should have_db_column(name). of_type(column_type). (:precision => precision, :limit => limit, :default => default, :null => null, :scale => scale, :sql_type => sql_type) end end |
#should_have_db_indices(*columns) ⇒ Object Also known as: should_have_db_index
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. Usetrue
to explicitly test for a unique constraint. Usefalse
to explicitly test for a non-unique constraint. Usenil
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
425 426 427 428 429 430 431 432 |
# File 'lib/shoulda/active_record/macros.rb', line 425 def should_have_db_indices(*columns) ::ActiveSupport::Deprecation.warn("use: should have_db_index") unique = (columns, :unique) columns.each do |column| should have_db_index(column).unique(unique) end end |
#should_have_instance_methods(*methods) ⇒ Object
Deprecated.
Ensure that the given instance methods are defined on the model.
should_have_instance_methods :email, :name, :name=
365 366 367 368 369 370 371 372 373 374 |
# File 'lib/shoulda/active_record/macros.rb', line 365 def should_have_instance_methods(*methods) ::ActiveSupport::Deprecation.warn (methods) klass = described_type methods.each do |method| should "respond to instance method ##{method}" do assert_respond_to klass.new, method, "#{klass.name} does not have instance method #{method}" end end end |
#should_have_many(*associations) ⇒ Object
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 forhas_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
284 285 286 287 288 289 290 |
# File 'lib/shoulda/active_record/macros.rb', line 284 def should_have_many(*associations) ::ActiveSupport::Deprecation.warn("use: should have_many") through, dependent = (associations, :through, :dependent) associations.each do |association| should have_many(association).through(through).dependent(dependent) end end |
#should_have_one(*associations) ⇒ Object
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
304 305 306 307 308 309 310 |
# File 'lib/shoulda/active_record/macros.rb', line 304 def should_have_one(*associations) ::ActiveSupport::Deprecation.warn("use: should have_one") dependent, through = (associations, :dependent, :through) associations.each do |association| should have_one(association).dependent(dependent).through(through) end end |
#should_have_readonly_attributes(*attributes) ⇒ Object
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
115 116 117 118 119 120 121 122 |
# File 'lib/shoulda/active_record/macros.rb', line 115 def should_have_readonly_attributes(*attributes) ::ActiveSupport::Deprecation.warn("use: should have_readonly_attribute") (attributes) attributes.each do |attribute| should have_readonly_attribute(attribute) end end |
#should_not_allow_mass_assignment_of(*attributes) ⇒ Object
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
100 101 102 103 104 105 106 107 |
# File 'lib/shoulda/active_record/macros.rb', line 100 def should_not_allow_mass_assignment_of(*attributes) ::ActiveSupport::Deprecation.warn("use: should_not allow_mass_assignment_of") (attributes) attributes.each do |attribute| should_not allow_mass_assignment_of(attribute) end end |
#should_not_allow_values_for(attribute, *bad_values) ⇒ Object
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 inerrors.on(:attribute)
. Regexp or string. If omitted, the test will pass if there is ANY error inerrors.on(:attribute)
.
Example:
should_not_allow_values_for :isbn, "bad 1", "bad 2"
136 137 138 139 140 141 142 |
# File 'lib/shoulda/active_record/macros.rb', line 136 def should_not_allow_values_for(attribute, *bad_values) ::ActiveSupport::Deprecation.warn("use: should_not allow_value") = (bad_values, :message) bad_values.each do |value| should_not allow_value(value).for(attribute).() end end |
#should_validate_acceptance_of(*attributes) ⇒ Object
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 inerrors.on(:attribute)
. Regexp or string. Default =I18n.translate('activerecord.errors.messages.accepted')
Example:
should_validate_acceptance_of :eula
447 448 449 450 451 452 453 454 |
# File 'lib/shoulda/active_record/macros.rb', line 447 def should_validate_acceptance_of(*attributes) ::ActiveSupport::Deprecation.warn("use: should validate_acceptance_of") = (attributes, :message) attributes.each do |attribute| should validate_acceptance_of(attribute).() end end |
#should_validate_numericality_of(*attributes) ⇒ Object
Deprecated: use ActiveRecord::Matchers#validate_numericality_of instead.
Ensure that the attribute is numeric
Options:
-
:message
- value the test expects to find inerrors.on(:attribute)
. Regexp or string. Default =I18n.translate('activerecord.errors.messages.not_a_number')
Example:
should_validate_numericality_of :age
260 261 262 263 264 265 266 267 |
# File 'lib/shoulda/active_record/macros.rb', line 260 def should_validate_numericality_of(*attributes) ::ActiveSupport::Deprecation.warn("use: should validate_numericality_of") = (attributes, :message) attributes.each do |attribute| should validate_numericality_of(attribute). () end end |
#should_validate_presence_of(*attributes) ⇒ Object
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 inerrors.on(:attribute)
. Regexp or string. Default =I18n.translate('activerecord.errors.messages.blank')
Example:
should_validate_presence_of :name, :phone_number
37 38 39 40 41 42 43 44 |
# File 'lib/shoulda/active_record/macros.rb', line 37 def should_validate_presence_of(*attributes) ::ActiveSupport::Deprecation.warn("use: should validate_presence_of") = (attributes, :message) attributes.each do |attribute| should validate_presence_of(attribute).() end end |
#should_validate_uniqueness_of(*attributes) ⇒ Object
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 inerrors.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
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/shoulda/active_record/macros.rb', line 65 def should_validate_uniqueness_of(*attributes) ::ActiveSupport::Deprecation.warn("use: should validate_uniqueness_of") , scope, case_sensitive = (attributes, :message, :scoped_to, :case_sensitive) scope = [*scope].compact case_sensitive = true if case_sensitive.nil? attributes.each do |attribute| matcher = validate_uniqueness_of(attribute). ().scoped_to(scope) matcher = matcher.case_insensitive unless case_sensitive should matcher end end |