2016-08-06 12:38:23 -04:00
|
|
|
require "cases/helper"
|
2010-05-08 16:27:49 -04:00
|
|
|
|
2016-08-06 12:38:23 -04:00
|
|
|
require "models/topic"
|
2010-05-08 16:27:49 -04:00
|
|
|
|
2010-05-08 17:03:45 -04:00
|
|
|
class ValidationsContextTest < ActiveModel::TestCase
|
2010-05-08 16:27:49 -04:00
|
|
|
def teardown
|
2014-01-27 05:22:26 -05:00
|
|
|
Topic.clear_validators!
|
2010-05-08 16:27:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
ERROR_MESSAGE = "Validation error from validator"
|
2015-09-07 17:41:50 -04:00
|
|
|
ANOTHER_ERROR_MESSAGE = "Another validation error from validator"
|
2010-05-08 16:27:49 -04:00
|
|
|
|
|
|
|
class ValidatorThatAddsErrors < ActiveModel::Validator
|
|
|
|
def validate(record)
|
|
|
|
record.errors[:base] << ERROR_MESSAGE
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-07 17:41:50 -04:00
|
|
|
class AnotherValidatorThatAddsErrors < ActiveModel::Validator
|
|
|
|
def validate(record)
|
|
|
|
record.errors[:base] << ANOTHER_ERROR_MESSAGE
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-04-21 12:00:38 -04:00
|
|
|
test "with a class that adds errors on create and validating a new model with no arguments" do
|
2013-05-01 20:10:06 -04:00
|
|
|
Topic.validates_with(ValidatorThatAddsErrors, on: :create)
|
2010-05-08 16:27:49 -04:00
|
|
|
topic = Topic.new
|
2013-04-21 12:00:38 -04:00
|
|
|
assert topic.valid?, "Validation doesn't run on valid? if 'on' is set to create"
|
2010-05-08 16:27:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "with a class that adds errors on update and validating a new model" do
|
2013-05-01 20:10:06 -04:00
|
|
|
Topic.validates_with(ValidatorThatAddsErrors, on: :update)
|
2010-05-08 16:27:49 -04:00
|
|
|
topic = Topic.new
|
|
|
|
assert topic.valid?(:create), "Validation doesn't run on create if 'on' is set to update"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with a class that adds errors on create and validating a new model" do
|
2013-05-01 20:10:06 -04:00
|
|
|
Topic.validates_with(ValidatorThatAddsErrors, on: :create)
|
2010-05-08 16:27:49 -04:00
|
|
|
topic = Topic.new
|
|
|
|
assert topic.invalid?(:create), "Validation does run on create if 'on' is set to create"
|
|
|
|
assert topic.errors[:base].include?(ERROR_MESSAGE)
|
|
|
|
end
|
2014-01-18 11:34:38 -05:00
|
|
|
|
|
|
|
test "with a class that adds errors on multiple contexts and validating a new model" do
|
|
|
|
Topic.validates_with(ValidatorThatAddsErrors, on: [:context1, :context2])
|
2014-01-27 05:17:20 -05:00
|
|
|
|
2014-01-18 11:34:38 -05:00
|
|
|
topic = Topic.new
|
2014-01-27 05:17:20 -05:00
|
|
|
assert topic.valid?, "Validation ran with no context given when 'on' is set to context1 and context2"
|
|
|
|
|
|
|
|
assert topic.invalid?(:context1), "Validation did not run on context1 when 'on' is set to context1 and context2"
|
2014-01-18 11:34:38 -05:00
|
|
|
assert topic.errors[:base].include?(ERROR_MESSAGE)
|
2014-01-27 05:17:20 -05:00
|
|
|
|
|
|
|
assert topic.invalid?(:context2), "Validation did not run on context2 when 'on' is set to context1 and context2"
|
2014-01-18 11:34:38 -05:00
|
|
|
assert topic.errors[:base].include?(ERROR_MESSAGE)
|
|
|
|
end
|
2015-09-07 17:41:50 -04:00
|
|
|
|
|
|
|
test "with a class that validating a model for a multiple contexts" do
|
|
|
|
Topic.validates_with(ValidatorThatAddsErrors, on: :context1)
|
|
|
|
Topic.validates_with(AnotherValidatorThatAddsErrors, on: :context2)
|
|
|
|
|
|
|
|
topic = Topic.new
|
|
|
|
assert topic.valid?, "Validation ran with no context given when 'on' is set to context1 and context2"
|
|
|
|
|
|
|
|
assert topic.invalid?([:context1, :context2]), "Validation did not run on context1 when 'on' is set to context1 and context2"
|
|
|
|
assert topic.errors[:base].include?(ERROR_MESSAGE)
|
|
|
|
assert topic.errors[:base].include?(ANOTHER_ERROR_MESSAGE)
|
|
|
|
end
|
2013-05-01 20:10:06 -04:00
|
|
|
end
|