Add support for validation contexts.
This commit is contained in:
parent
d1dc606cea
commit
d680e7d47b
2
NEWS.md
2
NEWS.md
|
@ -1,5 +1,7 @@
|
|||
# HEAD
|
||||
|
||||
* Support validation contexts for testing validations `on: :create` and when using custom contexts like `model.valid?(:my_context)`.
|
||||
|
||||
* Fix a bug in validations with autosaved models.
|
||||
|
||||
* Fix maximum value detection for the `ensure_inclusion_of` and
|
||||
|
|
|
@ -43,6 +43,11 @@ module Shoulda # :nodoc:
|
|||
self
|
||||
end
|
||||
|
||||
def on(context)
|
||||
@context = context
|
||||
self
|
||||
end
|
||||
|
||||
def with_message(message)
|
||||
self.options[:expected_message] = message
|
||||
self
|
||||
|
@ -78,7 +83,7 @@ module Shoulda # :nodoc:
|
|||
private
|
||||
|
||||
attr_accessor :values_to_match, :message_finder_factory,
|
||||
:instance, :attribute, :value, :matched_error
|
||||
:instance, :attribute, :context, :value, :matched_error
|
||||
|
||||
def errors_match?
|
||||
has_messages? && errors_for_attribute_match?
|
||||
|
@ -161,7 +166,7 @@ module Shoulda # :nodoc:
|
|||
end
|
||||
|
||||
def message_finder
|
||||
message_finder_factory.new(instance, attribute)
|
||||
message_finder_factory.new(instance, attribute, context)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,9 +3,10 @@ module Shoulda
|
|||
module ActiveModel
|
||||
# Finds message information from exceptions thrown by #valid?
|
||||
class ExceptionMessageFinder
|
||||
def initialize(instance, attribute)
|
||||
def initialize(instance, attribute, context=nil)
|
||||
@instance = instance
|
||||
@attribute = attribute
|
||||
@context = context
|
||||
end
|
||||
|
||||
def allow_description(allowed_values)
|
||||
|
@ -39,7 +40,7 @@ module Shoulda
|
|||
private
|
||||
|
||||
def validate_and_rescue
|
||||
@instance.valid?
|
||||
@instance.valid?(@context)
|
||||
[]
|
||||
rescue ::ActiveModel::StrictValidationFailed => exception
|
||||
[exception.message]
|
||||
|
|
|
@ -9,6 +9,11 @@ module Shoulda # :nodoc:
|
|||
@strict = false
|
||||
end
|
||||
|
||||
def on(context)
|
||||
@context = context
|
||||
self
|
||||
end
|
||||
|
||||
def strict
|
||||
@strict = true
|
||||
self
|
||||
|
|
|
@ -6,9 +6,10 @@ module Shoulda
|
|||
class ValidationMessageFinder
|
||||
include Helpers
|
||||
|
||||
def initialize(instance, attribute)
|
||||
def initialize(instance, attribute, context=nil)
|
||||
@instance = instance
|
||||
@attribute = attribute
|
||||
@context = context
|
||||
end
|
||||
|
||||
def allow_description(allowed_values)
|
||||
|
@ -58,7 +59,7 @@ module Shoulda
|
|||
end
|
||||
|
||||
def validate_instance
|
||||
@instance.valid?
|
||||
@instance.valid?(@context)
|
||||
@instance
|
||||
end
|
||||
end
|
||||
|
|
|
@ -56,6 +56,24 @@ describe Shoulda::Matchers::ActiveModel::AllowValueMatcher do
|
|||
end
|
||||
end
|
||||
|
||||
context "an attribute with a context-dependent validation" do
|
||||
context "without the validation context" do
|
||||
it "allows a bad value" do
|
||||
validating_format(:with => /abc/, :on => :customisable).should allow_value("xyz").for(:attr)
|
||||
end
|
||||
end
|
||||
|
||||
context "with the validation context" do
|
||||
it "allows a good value" do
|
||||
validating_format(:with => /abc/, :on => :customisable).should allow_value("abcde").for(:attr).on(:customisable)
|
||||
end
|
||||
|
||||
it "rejects a bad value" do
|
||||
validating_format(:with => /abc/, :on => :customisable).should_not allow_value("xyz").for(:attr).on(:customisable)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'an attribute with several validations' do
|
||||
let(:model) do
|
||||
define_model :example, :attr => :string do
|
||||
|
|
Loading…
Reference in New Issue