Kent S. wrote:
What would be the result of your assert statement? If you just want
to log an event then you can just
Actually the result isn’t really important - as long as it does
something “obvious” so that I know that there’s a problem.
Simplest would be to throw an exception. Maybe if I get really clever, I
could change the behaviour depending on whether we’re running in
development or production mode, but that’s not something I care about
I can clearly write something like your example snippet, but I would
prefer to write something simpler, more declarative and without the
duplication that scattering that snippet everywhere would result in.
But I still believe that this code belongs to your unit tests.
I’m certainly not arguing against unit tests. I want more though - the
opportunity to make assertions about the code during normal execution as
well as during testing (if a little paranoia is good, then lots of
paranoia is better
I’ll give you an example from the program that I’m writing right now. It
implements objects which can be locked, and which have a state
associated with them. If everything is working correctly, then objects
with the state “closed” should never be locked. I have, of course,
written a bunch of unit tests which check that this is always the case.
But I’m paranoid and would like to put something like this:
assert !(q.locked? && q.state == :closed)
at a few strategic locations within the code. Just in case there’s some
weird route which could end up with an invalid object I haven’t thought
of and covered in my unit tests. Of course if I do find such a route,
the first thing that I’ll do is write a test to cover it
It’s looking like there’s nothing like this out there already (unless
anyone knows better?) - so I guess that I’ll write it myself.
My main problem is coming up with a name, given that “assert” is already
taken (an assert in non-test Rails code maps to Breakpoint#assert), and
so is “validate” (by ActiveRecord).
Suggestions gratefully received!
Snetterton, Castle Combe, Cadwell Park…
Who says I have a one track mind?