title: How to mock Repository/Unit Of Work
body: <p>In my app I have generic repository connected to controller through UnitOfWork. I want to unit test my app. To make this I need to mock db connection.
  Can you tell me what should do? Mock repo? Mock repo and UnitOfWork? I'll be gratefull for any code snippets/suggestions.
  Here my Repo:</p>
  
  <pre><code>public class GenericRepository&lt;TEntity&gt; where TEntity : class
  {
      internal EquipmentEntities context;
      internal DbSet&lt;TEntity&gt; dbSet;
  
      public GenericRepository(EquipmentEntities context)
      {
          this.context = context;
          this.dbSet = context.Set&lt;TEntity&gt;();
      }
  
      public virtual IEnumerable&lt;TEntity&gt; Get(
          List&lt;Expression&lt;Func&lt;TEntity, bool&gt;&gt;&gt; filter,
          Func&lt;IQueryable&lt;TEntity&gt;, IOrderedQueryable&lt;TEntity&gt;&gt; orderBy = null,
          int? Page=0,
          params Expression&lt;Func&lt;TEntity, object&gt;&gt;[] included)
      {
  
          IQueryable&lt;TEntity&gt; query = dbSet;
  
          foreach(var z in included)
          {
              query=query.Include(z);
          }
          if (orderBy != null)
          {
              query = orderBy(query);
              query = query.Skip((Page.Value - 1) * 30).Take(30);
          }
          if (filter != null)
          {
              foreach (var z in filter)
              {
                  query = query.Where(z);
              }
          }
          return query.ToList();
      }
  
      public virtual TEntity GetByID(object id)
      {
          return dbSet.Find(id);
      }
  
      public virtual void Insert(TEntity entity)
      {
          dbSet.Add(entity);
      }
  
      public virtual void Delete(object id)
      {
          TEntity entityToDelete = dbSet.Find(id);
          Delete(entityToDelete);
      }
  
      public virtual void Delete(TEntity entityToDelete)
      {
          if (context.Entry(entityToDelete).State == EntityState.Detached)
          {
              dbSet.Attach(entityToDelete);
          }
          dbSet.Remove(entityToDelete);
      }
  
      public virtual void Update(TEntity entityToUpdate)
      {
          dbSet.Attach(entityToUpdate);
          context.Entry(entityToUpdate).State = EntityState.Modified;
      }
  }
  </code></pre>
  
  <p>and UnitOfWork:</p>
  
  <pre><code>public class UnitOfWork {
      private EquipmentEntities context = new EquipmentEntities();
      private GenericRepository&lt;Role&gt; RoleRepository;
      private GenericRepository&lt;Storage&gt; StorageRepository;
      private GenericRepository&lt;Device&gt; DeviceRepository;
      private GenericRepository&lt;DeviceInstance&gt; DeviceInstanceRepository;
      private GenericRepository&lt;DeviceUsage&gt; DeviceUsageRepository;
      private GenericRepository&lt;User&gt; UserRepository;
  
      public GenericRepository&lt;Role&gt; roleRepository
      {
          get
          {
              if (this.RoleRepository == null)
              {
                  this.RoleRepository = new GenericRepository&lt;Role&gt;(context);
              }
              return RoleRepository;
          }
      }
  
      /*
      * redundant code for other controllers
      */
      public void Save()
      {
          context.SaveChanges();
      }
  
      private bool disposed = false;
  
      protected virtual void Dispose(bool disposing)
      {
          if (!this.disposed)
          {
              if (disposing)
              {
                  context.Dispose();
              }
          }
          this.disposed = true;
      }
  
      public void Dispose()
      {
          Dispose(true);
          GC.SuppressFinalize(this);
      }
  }
  </code></pre>
  
  <p>sample controller:</p>
  
  <pre><code> public class UserController : Controller
  {
      //private EquipmentEntities db = new EquipmentEntities();
      private UnitOfWork unitOfWork = new UnitOfWork();
  
      // GET: /User/
      public ActionResult Index(string Name, string Surname, int? Page, string submit)
      {
          List&lt;Expression&lt;Func&lt;User, bool&gt;&gt;&gt; where = new List&lt;Expression&lt;Func&lt;User, bool&gt;&gt;&gt;();
          if (!string.IsNullOrEmpty(Name))
          {
              where.Add(w =&gt; w.Name.Contains(Name));
          }
          if (!string.IsNullOrEmpty(Surname))
          {
              where.Add(w =&gt; w.Surname.Contains(Surname));
          }
          var users = unitOfWork.userRepository.Get(where, null, Page, u =&gt; u.Role);
          return View(users);
      }
  
      // GET: /User/Details/5
      public ActionResult Details(int? id)
      {
          if (id == null)
          {
              return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
          }
          User user = unitOfWork.userRepository.GetByID(id.Value);
          //User user = db.Users.Find(id);
          if (user == null)
          {
              return HttpNotFound();
          }
          return View(user);
      }
  
      // GET: /User/Create
      public ActionResult Create()
      {
          ViewBag.RoleId = new SelectList(unitOfWork.roleRepository.Get(null), "Id", "RoleName");
          return View();
      }
  
      // POST: /User/Create
      // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
      // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
      [HttpPost]
      [ValidateAntiForgeryToken]
      public ActionResult Create([Bind(Include="Id,EmployeeNo,Name,Surname,ContactInfo,RoleId")] User user)
      {
          if (ModelState.IsValid)
          {
              unitOfWork.userRepository.Insert(user);
              unitOfWork.Save();
              return RedirectToAction("Index");
          }
          ViewBag.RoleId = new SelectList(unitOfWork.roleRepository.Get(null), "Id", "RoleName", user.RoleId);
          return View(user);
      }
  
      // GET: /User/Edit/5
      public ActionResult Edit(int? id)
      {
          if (id == null)
          {
              return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
          }
          User user = unitOfWork.userRepository.GetByID(id.Value);
          if (user == null)
          {
              return HttpNotFound();
          }
          ViewBag.RoleId = new SelectList(unitOfWork.roleRepository.Get(null), "Id", "RoleName", user.RoleId);
          return View(user);
      }
  
      // POST: /User/Edit/5
      // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
      // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
      [HttpPost]
      [ValidateAntiForgeryToken]
      public ActionResult Edit([Bind(Include="Id,EmployeeNo,Name,Surname,ContactInfo,RoleId")] User user)
      {
          if (ModelState.IsValid)
          {
              unitOfWork.userRepository.Update(user);
              unitOfWork.Save();
              return RedirectToAction("Index");
          }
          ViewBag.RoleId = new SelectList(unitOfWork.roleRepository.Get(null), "Id", "RoleName", user.RoleId);
          return View(user);
      }
  
      // GET: /User/Delete/5
      public ActionResult Delete(int? id)
      {
          if (id == null)
          {
              return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
          }
          User user = unitOfWork.userRepository.GetByID(id.Value);
          if (user == null)
          {
              return HttpNotFound();
          }
          if (unitOfWork.deviceUsageRepository.Get(null).Where(w =&gt; w.UserId == id) != null)
          {
              ViewBag.Error = 1;
              ModelState.AddModelError("", "Nie można kasować uyztkownika z przypisanymi urządzeniami");
  
          }
          else
          {
              ViewBag.Error = 0;
          }
          return View(user);
      }
  
      // POST: /User/Delete/5
      [HttpPost, ActionName("Delete")]
      [ValidateAntiForgeryToken]
      public ActionResult DeleteConfirmed(int id)
      {
          User user = unitOfWork.userRepository.GetByID(id);
          unitOfWork.deviceUsageRepository.Delete(user);
          unitOfWork.Save();
          return RedirectToAction("Index");
      }
  
      protected override void Dispose(bool disposing)
      {
          if (disposing)
          {
              unitOfWork.Save();
          }
          base.Dispose(disposing);
      }
  }
  </code></pre>