2014-10-14 05:16:12 +00:00
|
|
|
require_relative 'class_builder'
|
|
|
|
|
2014-10-27 00:51:42 +00:00
|
|
|
module UnitTests
|
|
|
|
module ModelBuilder
|
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 create_table(*args, &block)
|
|
|
|
ModelBuilder.create_table(*args, &block)
|
|
|
|
end
|
2014-10-14 05:16:12 +00:00
|
|
|
|
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 define_model(*args, &block)
|
|
|
|
ModelBuilder.define_model(*args, &block)
|
2010-12-13 22:28:59 +00:00
|
|
|
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 define_model_class(*args, &block)
|
|
|
|
ModelBuilder.define_model_class(*args, &block)
|
2014-10-27 00:51:42 +00:00
|
|
|
end
|
2014-10-14 05:16:12 +00:00
|
|
|
|
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 define_active_model_class(*args, &block)
|
|
|
|
ModelBuilder.define_active_model_class(*args, &block)
|
2008-12-15 18:13:56 +00:00
|
|
|
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
|
|
|
class << self
|
|
|
|
def configure_example_group(example_group)
|
|
|
|
example_group.include(self)
|
allow_value: Raise error if attr sets value differently
`allow_value` will now raise a CouldNotSetAttribute error if the
attribute in question cannot be changed from a non-nil value to a nil
value, or vice versa. In other words, these are the exact cases in which
the error will occur:
* If you're testing whether the attribute allows `nil`, but the
attribute detects and ignores nil. (For instance, you have a model
that `has_secure_password`. This will add a #password= method to your
model that is defined in a such a way that you cannot clear the
password by setting it to nil -- nothing happens.)
* If you're testing whether the attribute allows a non-nil value, but
the attribute fails to set that value. (For instance, you have an
ActiveRecord model. If ActiveRecord cannot typecast the value in the
context of the column, then it will do nothing, and the attribute will be
effectively set to nil.)
What's the reasoning behind this change? Simply put, if you are assuming
that the attribute is changing but in fact it is not, then the test
you're writing isn't the test that actually gets run. We feel that this
is dishonest and produces an invalid test.
2013-11-22 20:46:59 +00:00
|
|
|
|
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
|
|
|
example_group.after do
|
|
|
|
ModelBuilder.reset
|
2015-12-22 07:01:21 +00:00
|
|
|
end
|
|
|
|
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 reset
|
|
|
|
clear_column_caches
|
|
|
|
drop_created_tables
|
|
|
|
created_tables.clear
|
|
|
|
defined_models.clear
|
2011-01-24 16:19:34 +00:00
|
|
|
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 create_table(table_name, options = {}, &block)
|
|
|
|
connection = ::ActiveRecord::Base.connection
|
|
|
|
|
|
|
|
begin
|
|
|
|
connection.execute("DROP TABLE IF EXISTS #{table_name}")
|
|
|
|
connection.create_table(table_name, options, &block)
|
|
|
|
created_tables << table_name
|
|
|
|
connection
|
|
|
|
rescue Exception => e
|
|
|
|
connection.execute("DROP TABLE IF EXISTS #{table_name}")
|
|
|
|
raise e
|
2014-10-27 00:51:42 +00:00
|
|
|
end
|
2012-03-09 16:53:24 +00:00
|
|
|
end
|
2011-01-24 16:19:34 +00:00
|
|
|
|
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 define_model_class(class_name, &block)
|
|
|
|
ClassBuilder.define_class(class_name, ::ActiveRecord::Base, &block)
|
2014-10-27 00:51:42 +00:00
|
|
|
end
|
2014-10-08 05:19:07 +00:00
|
|
|
|
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 define_active_model_class(class_name, options = {}, &block)
|
|
|
|
attribute_names = options.delete(:accessors) { [] }
|
|
|
|
|
|
|
|
columns = attribute_names.reduce({}) do |hash, attribute_name|
|
|
|
|
hash.merge(attribute_name => nil)
|
2013-11-11 23:38:07 +00:00
|
|
|
end
|
2014-10-27 00:51:42 +00:00
|
|
|
|
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
|
|
|
UnitTests::ModelCreationStrategies::ActiveModel.call(
|
2019-01-30 05:58:03 +00:00
|
|
|
class_name,
|
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
|
|
|
columns,
|
|
|
|
options,
|
|
|
|
&block
|
|
|
|
)
|
2009-01-24 21:23:11 +00:00
|
|
|
end
|
2015-02-08 21:38:55 +00:00
|
|
|
|
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 define_model(name, columns = {}, options = {}, &block)
|
|
|
|
model = UnitTests::ModelCreationStrategies::ActiveRecord.call(
|
|
|
|
name,
|
|
|
|
columns,
|
|
|
|
options,
|
|
|
|
&block
|
|
|
|
)
|
|
|
|
defined_models << model
|
|
|
|
model
|
|
|
|
end
|
2015-02-08 21:38:55 +00:00
|
|
|
|
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
|
|
|
private
|
2009-01-24 21:23:11 +00:00
|
|
|
|
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 clear_column_caches
|
|
|
|
# Rails 4.x
|
|
|
|
if ::ActiveRecord::Base.connection.respond_to?(:schema_cache)
|
|
|
|
::ActiveRecord::Base.connection.schema_cache.clear!
|
|
|
|
# Rails 3.1 - 4.0
|
|
|
|
elsif ::ActiveRecord::Base.connection_pool.respond_to?(:clear_cache!)
|
|
|
|
::ActiveRecord::Base.connection_pool.clear_cache!
|
|
|
|
end
|
2012-12-20 05:04:27 +00:00
|
|
|
|
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
|
|
|
defined_models.each do |model|
|
|
|
|
model.reset_column_information
|
|
|
|
end
|
2014-11-06 17:28:13 +00:00
|
|
|
end
|
2015-02-08 21:38:55 +00:00
|
|
|
|
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 drop_created_tables
|
|
|
|
connection = ::ActiveRecord::Base.connection
|
2014-11-06 17:28:13 +00:00
|
|
|
|
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
|
|
|
created_tables.each do |table_name|
|
|
|
|
connection.execute("DROP TABLE IF EXISTS #{table_name}")
|
|
|
|
end
|
2014-10-27 00:51:42 +00:00
|
|
|
end
|
2014-10-14 05:16:12 +00:00
|
|
|
|
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 created_tables
|
|
|
|
@_created_tables ||= []
|
|
|
|
end
|
2015-02-08 21:38:55 +00:00
|
|
|
|
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 defined_models
|
|
|
|
@_defined_models ||= []
|
|
|
|
end
|
2015-02-08 21:38:55 +00:00
|
|
|
end
|
2014-10-14 05:16:12 +00:00
|
|
|
end
|
2010-12-13 22:28:59 +00:00
|
|
|
end
|