2016-01-20 66 views
2

如何使用NUNit和NSubstitute執行單元測試,我想測試插入僞造的「GamaItem」對象並驗證它是否有效,以及SaveChanges是否已觸發。使用NUnit和NSubstitute對通用存儲庫進行單元測試

Im新的單元測試,我不知道如何僞造dbContext對象。

在此先感謝。

工作單元:

public class UnitOfWork: IUnitOfWork, IDisposable 
{ 
    private SRColorContext context = new SRColorContext(); 
    private GenericEntityRepository<HairColorType> hairColorTypeRepository; 
    private GenericEntityRepository<GamaItem> gamaItemRepository; 

    public UnitOfWork() 
    { 
     if (context == null) 
     { 
      context = new SRColorContext(); 
     } 
    } 

    public UnitOfWork(SRColorContext context) 
    { 
     this.context = context; 
    } 


    public GenericEntityRepository<HairColorType> HairColorTypeRepository 
    { 
     get 
     { 
      if (this.hairColorTypeRepository == null) 
      { 
       this.hairColorTypeRepository = new GenericEntityRepository<HairColorType>(context); 
      } 
      return hairColorTypeRepository; 
     } 
    } 

    public GenericEntityRepository<GamaItem> GamaItemRepository 
    { 
     get 
     { 
      if (this.gamaItemRepository == null) 
      { 
       this.gamaItemRepository = new GenericEntityRepository<GamaItem>(context); 
      } 
      return gamaItemRepository; 
     } 
    } 

    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); 
    } 
} 

通用存儲庫:

public class GenericEntityRepository<TEntity> where TEntity : class 
{ 
    internal SRColorContext context; 
    internal DbSet<TEntity> dbSet; 

    public GenericEntityRepository(SRColorContext context) 
    { 
     this.context = context; 
     this.dbSet = context.Set<TEntity>(); 
    } 

    public virtual IEnumerable<TEntity> Get(
     Expression<Func<TEntity,bool>> filter = null, 
     Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, 
     string includeProperties = "") 
    { 
     IQueryable<TEntity> query = dbSet; 

     if(filter != null) 
     { 
      query = query.Where(filter); 
     } 

     foreach (var includeProperty in includeProperties.Split 
      (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) 
     { 
      query.Include(includeProperty); 
     } 

     if (orderBy != null) 
     { 
      return orderBy(query).ToList(); 
     } 
     else 
     { 
      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; 
    } 
} 
+0

測試基本上沒有任何保證。當從LINQ到SQL的轉換失敗時,大多數錯誤都會出現,並且一旦您開始使用類似的通用存儲庫,那麼這些錯誤將會在您的業務層發生。 – jgauffin

回答

0

我想你應該假上下文對象,以避免調用數據庫,而不是實體(除非你不能簡單的創建一個)

如果您想要編寫單元測試UnitOfWork類,並且您正在使用NSubtitute(或任何其他.NET OSS M ocking框架),您必須使用依賴注入並用假實例替換上下文實例。

您可能還需要包裝上下文類 - 如果密封或使用的方法不是虛擬的。

相關問題