title: How can I mock a repository and unit of work pattern in Entity Framework?
body: <p>I am new in Moq and Unit Testing. I want to test my Repository and Unit of Work pattern with entity framework 5. But I don't understand where and how can I start.</p>
  
  <p>My Repository Interface :</p>
  
  <pre><code>public interface ISmRepository&lt;T&gt;
  {
      void Add(T entity);
      void Remove(T entity);
      void Update(T entity);
      IQueryable&lt;T&gt; SearchFor(Expression&lt;Func&lt;T, bool&gt;&gt; expression);
      IQueryable&lt;T&gt; GetAll();
      T GetById(Int64 id);
  } 
  </code></pre>
  
  <p>My Repository:</p>
  
  <pre><code>public class SmReporitory&lt;T&gt; : ISmRepository&lt;T&gt; where T : class, IEntity, new()
  {
      private readonly DbSet&lt;T&gt; _dbSet;
      private readonly DbContext _dbContext;
  
      public SmReporitory(DbContext dbContext)
      {
          _dbSet = dbContext.Set&lt;T&gt;();
          _dbContext = dbContext;
      }
  
      public void Add(T entity)
      {
          _dbSet.Add(entity);
      }
  
      public void Remove(T entity)
      {
          _dbSet.Remove(entity);
      }
  
      public void Update(T entity)
      {
          _dbContext.Entry(entity).State = EntityState.Modified;
      }
  
      public IQueryable&lt;T&gt; SearchFor(Expression&lt;Func&lt;T, bool&gt;&gt; expression)
      {
          return _dbSet.Where(expression);
      }
  
      public IQueryable&lt;T&gt; GetAll()
      {
          return _dbSet;
      }
  
      public T GetById(long id)
      {
          return _dbSet.FirstOrDefault(x =&gt; x.Id == id);
      }
  }
  </code></pre>
  
  <p>My Unit Of Work Interface :</p>
  
  <pre><code>public interface ISmUnitOfWork : IDisposable
  {
      ISmRepository&lt;BreakdownCause&gt; BreakdownCasus { get; }
      ISmRepository&lt;BreakDownType&gt; BreakDownTypes { get; }
      ISmRepository&lt;CompanyInformation&gt; CompanyInformations { get; }
      void Save();
  }
  </code></pre>
  
  <p>My Unit Of Work Implementation :</p>
  
  <pre><code>public class SmUnitOfWork : ISmUnitOfWork
  {
      private readonly DbContext _dbContext;
      private ISmRepository&lt;BreakDownType&gt; _breakDownTypes;
      private ISmRepository&lt;BreakdownCause&gt; _breakdownCasus;
      private ISmRepository&lt;CompanyInformation&gt; _companyInformations;
  
      public SmUnitOfWork() : this(new SmDbContext())
      {
      }
  
      public SmUnitOfWork(SmDbContext smDbContext)
      {
          _dbContext = smDbContext;
      }
  
      public ISmRepository&lt;BreakdownCause&gt; BreakdownCasus
      {
          get { return _breakdownCasus ?? (_breakdownCasus = new SmReporitory&lt;BreakdownCause&gt;(_dbContext)); }
      }
  
      public ISmRepository&lt;BreakDownType&gt; BreakDownTypes
      {
          get { return _breakDownTypes ?? (_breakDownTypes = new SmReporitory&lt;BreakDownType&gt;(_dbContext)); }
      }
  
      public ISmRepository&lt;CompanyInformation&gt; CompanyInformations
      {
          get { return _companyInformations ?? (_companyInformations = new SmReporitory&lt;CompanyInformation&gt;(_dbContext)); }
      }
      public void Save()
      {
          try
          {
              _dbContext.SaveChanges();
          }
          catch
          {
              throw;
          }
      }
      public void Dispose()
      {
          if (_dbContext!=null)
          {
              _dbContext.Dispose();
          }
      }
  </code></pre>
  
  <p>Now I want to test ISmRepository interfaces method's.</p>
  
  <p>I already referenced NUnit and Moq in a class library project. Now I need a starting point.</p>