[mocha-developer] ActiveRecord Validations - validates_uniqueness_of

James Mead jamesmead44 at gmail.com
Fri Oct 6 06:10:27 EDT 2006

On 04/10/06, David Chelimsky <dchelimsky at gmail.com> wrote:
> There is a saying in the TDD community that you should test YOUR code,
> not other people's code. There is also another viewpoint that you
> should test behaviour, not implementation (BDD).

I'm sure both the ideas you mention above have been around for a long time
and are not necessarily related to a particular community. To me, the idea
of "not testing other people's code" mainly applies in the context of unit
testing. I would still want to have higher-level tests that test the system
as a whole.

To me, the idea of "testing behaviour, not implementation" is the difference
between black-box and white-box testing - not the difference between TDD and
BDD. I do not use a BDD framework, but my preference would always be to test
behaviour, because I think this leads to less brittle, more readable tests
where the intent is clear. However, I prefer to be pragmatic rather than
religious about avoiding testing the implementation.

What I would really want to ensure here is that Person validates
> uniqueness of email, not how it does it. What I would do is something
> like:
> def test_person_should_validate_uniqueness_of_email
>   assert_true Person.new(:email => "a at b.com").save
>   assert_false Person.new(:email => "a at b.com").save
> end
> The primary argument against this approach is that this test requires
> real interaction w/ the database and unit tests aren't supposed to
> interact w/ the database. While I usually agree w/ that perspective, I
> think that needs to be balanced against the fact that AR is very
> closely tied to the database. So my approach tends towards testing the
> behaviour (not the implementation) of model classes in the "unit
> tests" allowing database interaction, but mocking/stubbing that
> behaviour in "functional" and/or "integration" tests.
> I'm curioius to hear other people's perspective on this.

I agree with your assessment that because AR is very closely tied to the
database, it's sometimes hard to write tests that don't access the database.
However, your suggestion that unit tests should interact with the database,
but that functional or integration tests should not, seems very
back-to-front to me.

In a unit test context, I think it's valid to argue that
validates_uniqueness_of is third party code and  does not need to be tested.
So conceptually I think think it would be useful to be able to test simply
that validates_uniqueness_of is called from the Person class definition with
the expected parameters. Of course this assumes that
validates_uniqueness_ofis tested thoroughly in AR tests (or at least
it's being used by enough
people that we trust it to work!). If the uniqueness of a person's email
address is a critical business requirement of the system, I would expect a
high-level test to verify that requirement.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/mocha-developer/attachments/20061006/a6e82b7a/attachment.html 

More information about the mocha-developer mailing list