Add ValidationMatcherScenario for use in tests
Also add a helper method, `build_scenario_for_validation_matcher`, to all unit tests. Our plan is to slowly transition all of the tests to use this helper method, and that we we can get rid of the umpteen different ways we are building records across all of the tests.
This commit is contained in:
parent
85a3b03c30
commit
335971a87c
|
@ -0,0 +1,44 @@
|
|||
module UnitTests
|
||||
module ValidationMatcherScenarioHelpers
|
||||
def self.configure_example_group(example_group)
|
||||
example_group.include(self)
|
||||
end
|
||||
|
||||
def build_scenario_for_validation_matcher(args)
|
||||
UnitTests::ValidationMatcherScenario.new(
|
||||
build_validation_matcher_scenario_args(args)
|
||||
)
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def validation_matcher_scenario_args
|
||||
{}
|
||||
end
|
||||
|
||||
def configure_validation_matcher(matcher)
|
||||
matcher
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def build_validation_matcher_scenario_args(args)
|
||||
args.
|
||||
deep_merge(validation_matcher_scenario_args).
|
||||
deep_merge(
|
||||
matcher_name: matcher_name,
|
||||
matcher_proc: method(matcher_name)
|
||||
)
|
||||
end
|
||||
|
||||
def matcher_name
|
||||
validation_matcher_scenario_args.fetch(:matcher_name) do
|
||||
raise KeyNotFoundError.new(<<-MESSAGE)
|
||||
Please implement #validation_matcher_scenario_args in your example
|
||||
group, in such a way that it returns a hash that contains a
|
||||
:matcher_name key.
|
||||
MESSAGE
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,62 @@
|
|||
require 'forwardable'
|
||||
|
||||
module UnitTests
|
||||
class ValidationMatcherScenario
|
||||
extend Forwardable
|
||||
|
||||
attr_reader :matcher
|
||||
|
||||
def initialize(arguments)
|
||||
@arguments = arguments.dup
|
||||
@matcher_proc = @arguments.delete(:matcher_proc)
|
||||
|
||||
@specified_model_creator = @arguments.delete(:model_creator) do
|
||||
raise KeyError.new(<<-MESSAGE)
|
||||
:model_creator is missing. You can either provide it as an option or as
|
||||
a method.
|
||||
MESSAGE
|
||||
end
|
||||
|
||||
@model_creator = model_creator_class.new(@arguments)
|
||||
end
|
||||
|
||||
def record
|
||||
@_record ||= model.new.tap do |record|
|
||||
attribute_default_values_by_name.each do |attribute_name, default_value|
|
||||
record.public_send("#{attribute_name}=", default_value)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def model
|
||||
@_model ||= model_creator.call
|
||||
end
|
||||
|
||||
def matcher
|
||||
@_matcher ||= matcher_proc.call(attribute_name)
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
attr_reader(
|
||||
:arguments,
|
||||
:existing_value,
|
||||
:matcher_proc,
|
||||
:model_creator,
|
||||
:specified_model_creator,
|
||||
)
|
||||
|
||||
private
|
||||
|
||||
def_delegators(
|
||||
:model_creator,
|
||||
:attribute_name,
|
||||
:attribute_default_values_by_name,
|
||||
)
|
||||
|
||||
def model_creator_class
|
||||
UnitTests::ModelCreators.retrieve(specified_model_creator) ||
|
||||
specified_model_creator
|
||||
end
|
||||
end
|
||||
end
|
|
@ -23,6 +23,7 @@ RSpec.configure do |config|
|
|||
UnitTests::ActiveModelVersions.configure_example_group(config)
|
||||
UnitTests::DatabaseHelpers.configure_example_group(config)
|
||||
UnitTests::ColumnTypeHelpers.configure_example_group(config)
|
||||
UnitTests::ValidationMatcherScenarioHelpers.configure_example_group(config)
|
||||
|
||||
config.include UnitTests::Matchers
|
||||
|
||||
|
|
Loading…
Reference in New Issue