mirror of
https://github.com/thoughtbot/shoulda-matchers.git
synced 2022-11-09 12:01:38 -05:00
Add support for validation contexts.
This commit is contained in:
parent
d1dc606cea
commit
d680e7d47b
6 changed files with 38 additions and 6 deletions
2
NEWS.md
2
NEWS.md
|
@ -1,5 +1,7 @@
|
||||||
# HEAD
|
# 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 a bug in validations with autosaved models.
|
||||||
|
|
||||||
* Fix maximum value detection for the `ensure_inclusion_of` and
|
* Fix maximum value detection for the `ensure_inclusion_of` and
|
||||||
|
|
|
@ -43,6 +43,11 @@ module Shoulda # :nodoc:
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def on(context)
|
||||||
|
@context = context
|
||||||
|
self
|
||||||
|
end
|
||||||
|
|
||||||
def with_message(message)
|
def with_message(message)
|
||||||
self.options[:expected_message] = message
|
self.options[:expected_message] = message
|
||||||
self
|
self
|
||||||
|
@ -78,7 +83,7 @@ module Shoulda # :nodoc:
|
||||||
private
|
private
|
||||||
|
|
||||||
attr_accessor :values_to_match, :message_finder_factory,
|
attr_accessor :values_to_match, :message_finder_factory,
|
||||||
:instance, :attribute, :value, :matched_error
|
:instance, :attribute, :context, :value, :matched_error
|
||||||
|
|
||||||
def errors_match?
|
def errors_match?
|
||||||
has_messages? && errors_for_attribute_match?
|
has_messages? && errors_for_attribute_match?
|
||||||
|
@ -161,7 +166,7 @@ module Shoulda # :nodoc:
|
||||||
end
|
end
|
||||||
|
|
||||||
def message_finder
|
def message_finder
|
||||||
message_finder_factory.new(instance, attribute)
|
message_finder_factory.new(instance, attribute, context)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,9 +3,10 @@ module Shoulda
|
||||||
module ActiveModel
|
module ActiveModel
|
||||||
# Finds message information from exceptions thrown by #valid?
|
# Finds message information from exceptions thrown by #valid?
|
||||||
class ExceptionMessageFinder
|
class ExceptionMessageFinder
|
||||||
def initialize(instance, attribute)
|
def initialize(instance, attribute, context=nil)
|
||||||
@instance = instance
|
@instance = instance
|
||||||
@attribute = attribute
|
@attribute = attribute
|
||||||
|
@context = context
|
||||||
end
|
end
|
||||||
|
|
||||||
def allow_description(allowed_values)
|
def allow_description(allowed_values)
|
||||||
|
@ -39,7 +40,7 @@ module Shoulda
|
||||||
private
|
private
|
||||||
|
|
||||||
def validate_and_rescue
|
def validate_and_rescue
|
||||||
@instance.valid?
|
@instance.valid?(@context)
|
||||||
[]
|
[]
|
||||||
rescue ::ActiveModel::StrictValidationFailed => exception
|
rescue ::ActiveModel::StrictValidationFailed => exception
|
||||||
[exception.message]
|
[exception.message]
|
||||||
|
|
|
@ -9,6 +9,11 @@ module Shoulda # :nodoc:
|
||||||
@strict = false
|
@strict = false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def on(context)
|
||||||
|
@context = context
|
||||||
|
self
|
||||||
|
end
|
||||||
|
|
||||||
def strict
|
def strict
|
||||||
@strict = true
|
@strict = true
|
||||||
self
|
self
|
||||||
|
|
|
@ -6,9 +6,10 @@ module Shoulda
|
||||||
class ValidationMessageFinder
|
class ValidationMessageFinder
|
||||||
include Helpers
|
include Helpers
|
||||||
|
|
||||||
def initialize(instance, attribute)
|
def initialize(instance, attribute, context=nil)
|
||||||
@instance = instance
|
@instance = instance
|
||||||
@attribute = attribute
|
@attribute = attribute
|
||||||
|
@context = context
|
||||||
end
|
end
|
||||||
|
|
||||||
def allow_description(allowed_values)
|
def allow_description(allowed_values)
|
||||||
|
@ -58,7 +59,7 @@ module Shoulda
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_instance
|
def validate_instance
|
||||||
@instance.valid?
|
@instance.valid?(@context)
|
||||||
@instance
|
@instance
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -56,6 +56,24 @@ describe Shoulda::Matchers::ActiveModel::AllowValueMatcher do
|
||||||
end
|
end
|
||||||
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
|
context 'an attribute with several validations' do
|
||||||
let(:model) do
|
let(:model) do
|
||||||
define_model :example, :attr => :string do
|
define_model :example, :attr => :string do
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue