Method: Shoulda::Matchers::ActiveModel#validate_numericality_of
- Defined in:
- lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb
#validate_numericality_of(attr) ⇒ ValidateNumericalityOfMatcher
The ‘validate_numericality_of` matcher tests usage of the `validates_numericality_of` validation.
class Person
include ActiveModel::Model
attr_accessor :gpa
validates_numericality_of :gpa
end
# RSpec
RSpec.describe Person, type: :model do
it { should validate_numericality_of(:gpa) }
end
# Minitest (Shoulda)
class PersonTest < ActiveSupport::TestCase
should validate_numericality_of(:gpa)
end
#### Qualifiers
##### on
Use ‘on` if your validation applies only under a certain context.
class Person
include ActiveModel::Model
attr_accessor :number_of_dependents
validates_numericality_of :number_of_dependents, on: :create
end
# RSpec
RSpec.describe Person, type: :model do
it do
should validate_numericality_of(:number_of_dependents).
on(:create)
end
end
# Minitest (Shoulda)
class PersonTest < ActiveSupport::TestCase
should validate_numericality_of(:number_of_dependents).on(:create)
end
##### only_integer
Use ‘only_integer` to test usage of the `:only_integer` option. This asserts that your attribute only allows integer numbers and disallows non-integer ones.
class Person
include ActiveModel::Model
attr_accessor :age
validates_numericality_of :age, only_integer: true
end
# RSpec
RSpec.describe Person, type: :model do
it { should validate_numericality_of(:age).only_integer }
end
# Minitest (Shoulda)
class PersonTest < ActiveSupport::TestCase
should validate_numericality_of(:age).only_integer
end
##### is_less_than
Use ‘is_less_than` to test usage of the the `:less_than` option. This asserts that the attribute can take a number which is less than the given value and cannot take a number which is greater than or equal to it.
class Person
include ActiveModel::Model
attr_accessor :number_of_cars
validates_numericality_of :number_of_cars, less_than: 2
end
# RSpec
RSpec.describe Person, type: :model do
it do
should validate_numericality_of(:number_of_cars).
is_less_than(2)
end
end
# Minitest (Shoulda)
class PersonTest < ActiveSupport::TestCase
should validate_numericality_of(:number_of_cars).
is_less_than(2)
end
##### is_less_than_or_equal_to
Use ‘is_less_than_or_equal_to` to test usage of the `:less_than_or_equal_to` option. This asserts that the attribute can take a number which is less than or equal to the given value and cannot take a number which is greater than it.
class Person
include ActiveModel::Model
attr_accessor :birth_year
validates_numericality_of :birth_year, less_than_or_equal_to: 1987
end
# RSpec
RSpec.describe Person, type: :model do
it do
should validate_numericality_of(:birth_year).
is_less_than_or_equal_to(1987)
end
end
# Minitest (Shoulda)
class PersonTest < ActiveSupport::TestCase
should validate_numericality_of(:birth_year).
is_less_than_or_equal_to(1987)
end
##### is_equal_to
Use ‘is_equal_to` to test usage of the `:equal_to` option. This asserts that the attribute can take a number which is equal to the given value and cannot take a number which is not equal.
class Person
include ActiveModel::Model
attr_accessor :weight
validates_numericality_of :weight, equal_to: 150
end
# RSpec
RSpec.describe Person, type: :model do
it { should validate_numericality_of(:weight).is_equal_to(150) }
end
# Minitest (Shoulda)
class PersonTest < ActiveSupport::TestCase
should validate_numericality_of(:weight).is_equal_to(150)
end
##### is_greater_than_or_equal_to
Use ‘is_greater_than_or_equal_to` to test usage of the `:greater_than_or_equal_to` option. This asserts that the attribute can take a number which is greater than or equal to the given value and cannot take a number which is less than it.
class Person
include ActiveModel::Model
attr_accessor :height
validates_numericality_of :height, greater_than_or_equal_to: 55
end
# RSpec
RSpec.describe Person, type: :model do
it do
should validate_numericality_of(:height).
is_greater_than_or_equal_to(55)
end
end
# Minitest (Shoulda)
class PersonTest < ActiveSupport::TestCase
should validate_numericality_of(:height).
is_greater_than_or_equal_to(55)
end
##### is_greater_than
Use ‘is_greater_than` to test usage of the `:greater_than` option. This asserts that the attribute can take a number which is greater than the given value and cannot take a number less than or equal to it.
class Person
include ActiveModel::Model
attr_accessor :legal_age
validates_numericality_of :legal_age, greater_than: 21
end
# RSpec
RSpec.describe Person, type: :model do
it do
should validate_numericality_of(:legal_age).
is_greater_than(21)
end
end
# Minitest (Shoulda)
class PersonTest < ActiveSupport::TestCase
should validate_numericality_of(:legal_age).
is_greater_than(21)
end
##### is_other_than
Use ‘is_other_than` to test usage of the `:other_than` option. This asserts that the attribute can take a number which is not equal to the given value.
class Person
include ActiveModel::Model
attr_accessor :legal_age
validates_numericality_of :legal_age, other_than: 21
end
# RSpec
RSpec.describe Person, type: :model do
it do
should validate_numericality_of(:legal_age).
is_other_than(21)
end
end
# Minitest (Shoulda)
class PersonTest < ActiveSupport::TestCase
should validate_numericality_of(:legal_age).
is_other_than(21)
end
##### even
Use ‘even` to test usage of the `:even` option. This asserts that the attribute can take odd numbers and cannot take even ones.
class Person
include ActiveModel::Model
attr_accessor :birth_month
validates_numericality_of :birth_month, even: true
end
# RSpec
RSpec.describe Person, type: :model do
it { should validate_numericality_of(:birth_month).even }
end
# Minitest (Shoulda)
class PersonTest < ActiveSupport::TestCase
should validate_numericality_of(:birth_month).even
end
##### odd
Use ‘odd` to test usage of the `:odd` option. This asserts that the attribute can take a number which is odd and cannot take a number which is even.
class Person
include ActiveModel::Model
attr_accessor :birth_day
validates_numericality_of :birth_day, odd: true
end
# RSpec
RSpec.describe Person, type: :model do
it { should validate_numericality_of(:birth_day).odd }
end
# Minitest (Shoulda)
class PersonTest < ActiveSupport::TestCase
should validate_numericality_of(:birth_day).odd
end
##### is_in
Use ‘is_in` to test usage of the `:in` option. This asserts that the attribute can take a number which is contained in the given range.
class Person
include ActiveModel::Model
attr_accessor :legal_age
validates_numericality_of :birth_month, in: 1..12
end
# RSpec
RSpec.describe Person, type: :model do
it do
should validate_numericality_of(:birth_month).
is_in(1..12)
end
end
# Minitest (Shoulda)
class PersonTest < ActiveSupport::TestCase
should validate_numericality_of(:birth_month).
is_in(1..12)
end
##### with_message
Use ‘with_message` if you are using a custom validation message.
class Person
include ActiveModel::Model
attr_accessor :number_of_dependents
validates_numericality_of :number_of_dependents,
message: 'Number of dependents must be a number'
end
# RSpec
RSpec.describe Person, type: :model do
it do
should validate_numericality_of(:number_of_dependents).
('Number of dependents must be a number')
end
end
# Minitest (Shoulda)
class PersonTest < ActiveSupport::TestCase
should validate_numericality_of(:number_of_dependents).
('Number of dependents must be a number')
end
##### allow_nil
Use ‘allow_nil` to assert that the attribute allows nil.
class Post
include ActiveModel::Model
attr_accessor :age
validates_numericality_of :age, allow_nil: true
end
# RSpec
RSpec.describe Post, type: :model do
it { should validate_numericality_of(:age).allow_nil }
end
# Minitest (Shoulda)
class PostTest < ActiveSupport::TestCase
should validate_numericality_of(:age).allow_nil
end
355 356 357 |
# File 'lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb', line 355 def validate_numericality_of(attr) ValidateNumericalityOfMatcher.new(attr) end |