title: How to test Repository Pattern with ADO.NET Entity Framework?
body: <p>While using Repository pattern I find it difficult to understand the reason of designing the software with TDD technique while in reality you will have to implement the Interface for your repository in your persistence dataset.</p>
  
  <p>To make my point clear I will submit an example:</p>
  
  <p>I have the following Interface on my domain model:</p>
  
  <pre><code>public interface IUserRepository
  {
      IQueryable&lt;User&gt; FindAllUsers();
      void AddUser(User newUser);
      User GetUserByID(int userID);
      void Update(User userToUpdate);
  }
  </code></pre>
  
  <p>I have the following implementation of the interface for testing purposes:</p>
  
  <pre><code>public class FakeUserRepository : IUserRepository
  {
      private IList&lt;User&gt; _repository;
  
      public FakeUserRepository()
      {
          _repository = new List&lt;User&gt;();
          ... //create all users for testing purposes
  
      }
  
      public IQueryable&lt;User&gt; FindAllUsers()
      {
          return _repository.AsQueryable&lt;User&gt;(); //returns all users
      }
  </code></pre>
  
  <p>Now I create a few tests: </p>
  
  <ol>
  <li>Can_Add_User</li>
  <li>Can_Add_Account for a User</li>
  <li>Can_Add_ShareSpace for a an account of a user with another user.</li>
  </ol>
  
  <p>My question is, after I test all these with my FakeUserRepository implementation, I have to go back and implement the IUserRepository on my actual persistence dataset (i.g. SQL), and I have to implement again the code, so my unit testing is not actually checking the code that I am actually using on my application.</p>
  
  <p>Maybe I am missing something.</p>
  
  <p>Thanks as always!</p>
  
  <p>Below then my Persistent data access repository which is the one that is supposed to be under test (by my mind at least), but then I should not test hooked to the database: </p>
  
  <pre><code>public class SQLUserRepository : IUserRepository
  {
      private BusinessDomainModel.EntityModel.BusinessHelperAccountDBEntities _repository;
  
      public SQLUserRepository()
      {
          _repository = new BusinessHelperAccountDBEntities();
      }
  
      #region IUserRepository Members
  
      public IQueryable&lt;User&gt; FindAllUsers()
      {
          return _repository.UserSet.AsQueryable();
      }
  </code></pre>