[mocha-developer] ActiveRecord Validations - validates_uniqueness_of

David Chelimsky dchelimsky at gmail.com
Wed Oct 4 10:49:45 EDT 2006

On 9/12/06, Jonathan Younger <daikini at gmail.com> wrote:
> Hello,
> I'm wanting to use Mocha and Stubba for my tests in place of the
> default Rails testing with fixtures.
> I have the following class:
> class Person < ActiveRecord::Base
>    validates_uniqueness_of :email
> end
> and am wondering how I can test to ensure that the
> validates_uniqueness_of validation is running.

I realize that this is a pretty old thread, but I was just catching up
and wanted to respond to this:

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).

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

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

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.


More information about the mocha-developer mailing list