mirror of
https://github.com/thoughtbot/shoulda-matchers.git
synced 2022-11-09 12:01:38 -05:00
85a3b03c30
The main driver behind this commit is to provide a programmatic way to define models in tests. We already have ways of doing this, of course, with `define_model` and `define_active_model_class`, but these methods are very low-level, and in writing tests we have historically made our own methods inside of test files to define full and complete models. So we have this common pattern of defining a model with a validation, and that's repeated across many different files. What we would like to do, right now, is extract some commonly used assertions to a shared example group. These assertions need to define models inside of the tests, but the issue is that sometimes the models are ActiveRecord models, and sometimes they are ActiveModel models, and when the shared example group is used within a test file, we need a way to choose the strategy we'd like to use at runtime. Since the way we currently define models is via methods, we can't really provide a strategy very easily. Also, if we need to customize how those models are defined (say, the attribute needs to be a has-many association instead of a normal attribute) then the methods only go so far in providing us that level of customization before things get really complicated. So, to help us with this, this commit takes the pattern of model-plus-validation previously mentioned and places it in multiple classes. Note that this is also a precursor to a later commit in which we introduce `ignoring_interference_by_writer` across the board. The way we will do this is by adding a shared example group that then uses these model creation classes internally to build objects instead of relying upon methods that the outer example group -- to which the shared example group is being mixed into -- provides.
90 lines
2.3 KiB
Ruby
90 lines
2.3 KiB
Ruby
module UnitTests
|
|
module ClassBuilder
|
|
def define_module(*args, &block)
|
|
ClassBuilder.define_module(*args, &block)
|
|
end
|
|
|
|
def define_class(*args, &block)
|
|
ClassBuilder.define_class(*args, &block)
|
|
end
|
|
|
|
class << self
|
|
def configure_example_group(example_group)
|
|
example_group.include(self)
|
|
|
|
example_group.after do
|
|
ClassBuilder.reset
|
|
end
|
|
end
|
|
|
|
def reset
|
|
remove_defined_classes
|
|
end
|
|
|
|
def define_module(module_name, &block)
|
|
module_name = module_name.to_s.camelize
|
|
|
|
namespace, name_without_namespace =
|
|
ClassBuilder.parse_constant_name(module_name)
|
|
|
|
if namespace.const_defined?(name_without_namespace, false)
|
|
namespace.__send__(:remove_const, name_without_namespace)
|
|
end
|
|
|
|
eval <<-RUBY
|
|
module #{namespace}::#{name_without_namespace}
|
|
end
|
|
RUBY
|
|
|
|
namespace.const_get(name_without_namespace).tap do |constant|
|
|
constant.unloadable
|
|
|
|
if block
|
|
constant.module_eval(&block)
|
|
end
|
|
end
|
|
end
|
|
|
|
def define_class(class_name, parent_class = Object, &block)
|
|
class_name = class_name.to_s.camelize
|
|
|
|
namespace, name_without_namespace =
|
|
ClassBuilder.parse_constant_name(class_name)
|
|
|
|
if namespace.const_defined?(name_without_namespace, false)
|
|
namespace.__send__(:remove_const, name_without_namespace)
|
|
end
|
|
|
|
eval <<-RUBY
|
|
class #{namespace}::#{name_without_namespace} < ::#{parent_class}
|
|
end
|
|
RUBY
|
|
|
|
namespace.const_get(name_without_namespace).tap do |constant|
|
|
constant.unloadable
|
|
|
|
if block
|
|
if block.arity == 0
|
|
constant.class_eval(&block)
|
|
else
|
|
block.call(constant)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
def parse_constant_name(name)
|
|
namespace = Shoulda::Matchers::Util.deconstantize(name)
|
|
qualified_namespace = (namespace.presence || 'Object').constantize
|
|
name_without_namespace = name.to_s.demodulize
|
|
[qualified_namespace, name_without_namespace]
|
|
end
|
|
|
|
private
|
|
|
|
def remove_defined_classes
|
|
::ActiveSupport::Dependencies.clear
|
|
end
|
|
end
|
|
end
|
|
end
|