title: NHibernate Unit Testing Mocking/In Memory Database
body: <p>I am trying to learn how to mock my generic repository so i can unit test all my services.</p>
  
  <p>Im using NHibernate Fluent to handle data access and a Ninject for dependency (I'm not interested in testing that) </p>
  
  <p>My repository interface looks like:</p>
  
  <pre><code>public interface IRepository&lt;TEntity&gt;  where TEntity : class 
  {
      IQueryable&lt;TEntity&gt; GetAll();
      TEntity Get(int key);
      void Insert(TEntity entity);
      void Update(TEntity entity);
      void Delete(int id);
  }
  </code></pre>
  
  <p>And the actual repository looks like:</p>
  
  <pre><code>public class GenerRepository&lt;TEntity&gt; : IRepository&lt;TEntity&gt;where TEntity : Entity
  {
      protected ISession Session{get { return NHibernateHelper.OpenSession(); }}
      public IQueryable&lt;TEntity&gt; GetAll(){return Session.Query&lt;TEntity&gt;();}
      public TEntity Get(int key){return Session.Get&lt;TEntity&gt;(key);}
      public void Insert(TEntity entity){Session.Save(entity);}
      public void Update(TEntity entity){Session.Update(entity);}
      public void Delete(int id){Session.Delete(Session.Load&lt;TEntity&gt;(id));}
  }
  </code></pre>
  
  <p>All my services do the following take the created repository in and use it.</p>
  
  <p>I've read so many articles on how to do this but none are simple or well explained. So any advice between creating a test generic repository or even mocking it. I would also be interested in creating a in memory database but how do i set the configuration up for fluent nhibernate in my test project without editing code in my real project?</p>
  
  <p>Is it possible just to make the generic repository hit a list of Tentity rather than the database or in memory database.</p>
  
  <p>Thanks for reading and look forward to the advice.</p>