如何模拟存储库/工作单元

szp*_*pic 8 c# unit-testing mocking

在我的应用程序中,我通过UnitOfWork连接到控制器的通用存储库.我想对我的应用进行单元测试.为此,我需要模拟数据库连接.你能告诉我应该怎么做吗?模拟回购?模拟回购和UnitOfWork?我会感激任何代码片段/建议.在这里我的回购:

public class GenericRepository<TEntity> where TEntity : class
{
    internal EquipmentEntities context;
    internal DbSet<TEntity> dbSet;

    public GenericRepository(EquipmentEntities context)
    {
        this.context = context;
        this.dbSet = context.Set<TEntity>();
    }

    public virtual IEnumerable<TEntity> Get(
        List<Expression<Func<TEntity, bool>>> filter,
        Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
        int? Page=0,
        params Expression<Func<TEntity, object>>[] included)
    {

        IQueryable<TEntity> 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

和UnitOfWork:

public class UnitOfWork {
    private EquipmentEntities context = new EquipmentEntities();
    private GenericRepository<Role> RoleRepository;
    private GenericRepository<Storage> StorageRepository;
    private GenericRepository<Device> DeviceRepository;
    private GenericRepository<DeviceInstance> DeviceInstanceRepository;
    private GenericRepository<DeviceUsage> DeviceUsageRepository;
    private GenericRepository<User> UserRepository;

    public GenericRepository<Role> roleRepository
    {
        get
        {
            if (this.RoleRepository == null)
            {
                this.RoleRepository = new GenericRepository<Role>(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);
    }
}
Run Code Online (Sandbox Code Playgroud)

样品控制器:

 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<Expression<Func<User, bool>>> where = new List<Expression<Func<User, bool>>>();
        if (!string.IsNullOrEmpty(Name))
        {
            where.Add(w => w.Name.Contains(Name));
        }
        if (!string.IsNullOrEmpty(Surname))
        {
            where.Add(w => w.Surname.Contains(Surname));
        }
        var users = unitOfWork.userRepository.Get(where, null, Page, u => 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 => 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

Maa*_*ten 12

不幸的GenericRepository<T>是,如果与您的存储库紧密耦合,您的上下文与UnitOfWork实现紧密相关.这使得它无法模仿它.

你必须引入松耦合:

  • 添加一个接口IRepository<T>,并在您的GenericRepository<T>类中实现
  • 添加界面IUnitOfWork并在您的UnitOfWork课程中实现此功能
  • IUnitOfWork接口仅是指IRepository<T>,GenericRepository<T>
  • 更新您的控制器构造函数以期望IUnitOfWork而不是UnitOfWork.
  • 最好的方法是在您的工作单元中注入存储库,但这意味着很多构造函数参数,并且在您可能没有使用它时,您将拥有它的实例.我想到的解决方案是IRepositoryFactory(具有相应的实现),这将允许您按需创建特定的存储库.工厂将有一个通用Create方法来创建通用存储库.然后可以将此工厂注入您的工作单元实施中.

现在,您可以模拟工作单元和/或存储库的每个部分.

更新

我在上面的文本中删除了存储库工厂,以及下面的代码.原因是,当我尝试创建伪代码时,我将上下文传递给泛型存储库时遇到了一些麻烦,因为存储库工厂不知道这个对象.而且,由于工作单元通用存储库都是紧密耦合的(因为它们共享上下文对象),我提出了以下解决方案:

public interface IRepository<TEntity> where TEntity: class {
    // Your methods
}
public class GenericRepository<TEntity> : IRepository<TEntity> where TEntity : class {
    public GenericRepository<TEntity>(EquipmentEntities  context) {
        // Your constructor
    }

    // Your implementation
}

public interface IUnitOfWork : IDisposable {
    IRepository<Role> RoleRepository { get; }
    IRepository<Storage> StorageRepository { get; }
    // etc

    void Save();
}

public class UnitOfWork : IUnitOfWork {
    public UnitOfWork () {
        this.context = new EquipmentEntities ();
    }

    private EquipmentEntities context = null;

    private IRepository<Role> roleRepository;
    public IRepository<Role> RoleRepository { 
        get {
            if (this.roleRepository == null) {
                this.roleRepository = new GenericRepository<Role>(context);
            }
            return this.roleRepository;
        }
    }

    // etc... other repositories
    // etc... your implementation for Save and Dispose
}
Run Code Online (Sandbox Code Playgroud)