Fixtures and validation

Hello,

I’m pretty new to Rails - very impressed not only by how much faster I
can
create working programs but also by how it helps me to think more
clearly
about design.

What has me confused is why does the fixture loading process bypass
model
validation? There must be a good reason for this, but I’ve been digging
around for hours and can’t find any explanation.

Thanks!

Chris

On 19 December 2011 02:47, Chris D. [email protected] wrote:

Hello,

I’m pretty new to Rails - very impressed not only by how much faster I can
create working programs but also by how it helps me to think more clearly
about design.

What has me confused is why does the fixture loading process bypass model
validation? There must be a good reason for this, but I’ve been digging
around for hours and can’t find any explanation.

The purpose of fixtures is for testing, if you decide that you want
something in the database for testing then that is up to you, even if
it might seem that there should be no way of getting that data under
normal operation. Remember that one might wish to test against legacy
data in a database that was added before some validation was added.
Also there may be routes other than rails for getting data there,
bypassing the validations.

Why do you care that fixtures bypass validations?

As a side note fixtures are generally considered to not be the best
way of testing. Most now use Factories I believe.

I advise newcomers to ignore fixtures and go straight to Factories.

Colin

Hi Colin,

Thanks for the response. Good point about legacy data. In fact, I’m
working on a legacy system that I’d like to start integrating Rails
into.
Would you happen to know of resources that focus on that?

Why do I care that fixtures bypass validations? I’ve been using Agile
Web
Development with Rails 4th Ed. to learn Rails. On page 83 it says “a
test
fixture is simply a specification of the initial contents of a model (or
models) under test.” I was assuming that to the degree that statement
is
accurate, the validations coded in the model would be run against the
data
loaded by the fixtures. Then, when I saw that wasn’t happening, I was
surprised to find no statements about running or not running
validations.
For example,see Testing Rails Applications — Ruby on Rails Guides

Everything you pointed out (“the purpose of fixtures…”) makes sense.
But I can also see that it would be useful to have a rake parm that
lets
the developer control whether or not validations are run on the
fixtures.

From what I’ve been reading, and one of the reasons I’m finding Rails so
appealing, is that (as DHH said) Rails is an opinionated framework and
has
a clear and strong stance on how to build good software. With that in
mind, I was hoping to learn the reasons behind why fixtures work the way
they do.

Thanks again for your response.

Chris