【问题标题】:Unit test of generic repository using NUnit and NSubstitute使用 NUnit 和 NSubstitute 对通用存储库进行单元测试
【发布时间】:2016-01-20 16:37:31
【问题描述】:

如何使用 NUUnit 和 NSubstitute 执行单元测试,我想测试插入一个假的“GamaItem”对象,并验证它是否有效,以及 SaveChanges 是否已被触发。

我是单元测试的新手,我不确定如何伪造 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;
    }
}

【问题讨论】:

  • 测试基本上不能保证什么。大多数错误会在从 LINQ 到 SQL 的转换失败时出现,而一旦您开始使用这样的通用存储库,这些错误就会发生在您的业务层中。

标签: c# entity-framework unit-testing nsubstitute


【解决方案1】:

我认为你应该伪造上下文对象以避免调用数据库而不是实体(除非你不能简单地创建一个)

如果您想为 UnitOfWork 类编写单元测试并且您使用 NSubtitute(或任何其他 .NET OSS 模拟框架),您必须使用依赖注入并将上下文实例替换为假实例。

您可能还需要包装上下文类 - 如果它是密封的或使用的方法不是虚拟的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-17
    相关资源
    最近更新 更多