title: Testing the persistence layer in a DDD/TDD application
body: <p>If I have the following domain object:</p>
  
  <pre><code>public class Customer
  {
      public virtual Guid Id { get; set; }
      public virtual string Name { get; set; }
      public virtual ISet&lt;Order&gt; Orders { get; set; }
  
      public Customer()
      {
          Orders = new HashedSet&lt;Order&gt;();
      }
  
      public virtual void AddOrder(Order order)
      {
          order.Customer = this;
          Orders.Add(order);
      }
  }
  </code></pre>
  
  <p>with the following NHibernate mapping:</p>
  
  <pre><code>&lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Examples" assembly="Examples"&gt;
    &lt;class name="Customer"&gt;
        &lt;id name="Id"&gt;
          &lt;generator class="guid.comb" /&gt;
        &lt;/id&gt;
        &lt;property name="Name" length="50"/&gt;
        &lt;set name="Orders" table="CustomerOrder" cascade="all-delete-orphan" lazy="true"&gt;
          &lt;key column="CustomerId"/&gt;
          &lt;many-to-many class="Order" column="OrderId"/&gt;
        &lt;/set&gt;
    &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>Is there any value in this test?</p>
  
  <pre><code>[Test]
  public Save_NameWritten_SameNameIsReadback()
  {
      var expected = new Customer { Name = "Fred Quimby" };
      _repo.Save(c);
      var actual = _repo.Find(expected.Id);
      Assert.AreEqual(expected.Name, actual.Name);
  }
  </code></pre>
  
  <p>Do folks commonly test their persistence layer like this? Making sure that each field is persisted individually? I'm honestly not sure what best practice is for something like this. I can see testing something with long strings and parent/child relationships - but what about integers and dates? Is this overkill?</p>
  
  <p>I'm just talking about the persistence layer here, not the business logic in the domain layer. For that, I would mock the repository, whereas here I'm verifying that the repository actually saved the thing that I told it to save. What if someone forgets to map a field, or they have a bogus string length in the mapping?</p>
  
  <p>Are there any tools to automatically generate these kinds of tests in .NET? Or is that "bad"?</p>