thoughtbot--shoulda-matchers/spec/support/unit/model_creation_strategies/active_record.rb

94 lines
2.0 KiB
Ruby
Raw Normal View History

Extract classes for defining models in tests 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.
2015-12-23 23:10:40 +00:00
module UnitTests
module ModelCreationStrategies
class ActiveRecord
def self.call(name, columns = {}, options = {}, &block)
new(name, columns, options, &block).call
end
def initialize(name, columns = {}, options = {}, &block)
@name = name
@columns = columns
@options = options
@model_customizers = []
if block
customize_model(&block)
end
end
def customize_model(&block)
model_customizers << block
end
def call
create_table_for_model
define_class_for_model
end
protected
attr_reader :columns, :model_customizers, :name, :options
private
def create_table_for_model
UnitTests::ActiveRecord::CreateTable.call(
table_name: table_name,
columns: columns,
connection: parent_class.connection,
&customize_table
)
Extract classes for defining models in tests 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.
2015-12-23 23:10:40 +00:00
end
def define_class_for_model
model = UnitTests::ModelBuilder.define_model_class(
class_name,
parent_class: parent_class
)
Extract classes for defining models in tests 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.
2015-12-23 23:10:40 +00:00
model_customizers.each do |block|
run_block(model, block)
end
if whitelist_attributes? && model.respond_to?(:attr_accessible)
Extract classes for defining models in tests 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.
2015-12-23 23:10:40 +00:00
model.attr_accessible(*columns.keys)
end
model.table_name = table_name
model
end
def run_block(model, block)
if block
if block.arity == 0
model.class_eval(&block)
else
block.call(model)
end
end
end
def class_name
name.to_s.pluralize.classify
end
def table_name
class_name.tableize.gsub('/', '_')
end
def parent_class
options.fetch(:parent_class, DevelopmentRecord)
end
def customize_table
options.fetch(:customize_table) { proc {} }
end
Extract classes for defining models in tests 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.
2015-12-23 23:10:40 +00:00
def whitelist_attributes?
options.fetch(:whitelist_attributes, true)
end
end
end
end