实体框架4.1的假的DbContext来测试我的资料库我的、资料库、实体、框架

2023-09-04 00:34:32 作者:姐丑的自然

我有一个基本信息库和所有的实体仓库从继承。

在我的睾丸我创建了一个假的DbContext和假DbSet测试我的资料库,但实施我FakeDbContext一些方法时,我没能实现的 IDbContext.Entry 方式:

 公共类FakeDbContext:IDbContext
{
    私人IDbSet< Usuario> _usuario;
    私人IDbSet< Atividade> _atividade;
    私人IDbSet<作者日期> _autor;
    私人IDbSet< CategoriaModulo> _categoriaModulo;
    私人IDbSet< CategoriaMateria> _categoriaMateria;
    私人IDbSet<网站> _现场;
    私人IDbSet<模> _modulo;
    私人IDbSet< PERFIL> _perfil;
    私人IDbSet< CategoriaGaleriaImagem> _categoriaGaleriaImagem;

    公共IDbSet< Usuario> Usuario {{返回_usuario? (_usuario =新FakeDbSet&其中; Usuario>()); } 组 { } }
    公共IDbSet< Atividade> Atividade {{返回_atividade? (_atividade =新FakeDbSet&其中; Atividade>()); } 组 { } }
    公共IDbSet<作者日期>作者日期{{返回_autor? (_autor =新FakeDbSet&其中;作者日期>()); } 组 { } }
    公共IDbSet< CategoriaModulo> CategoriaModulo {{返回_categoriaModulo? (_categoriaModulo =新FakeDbSet&其中; CategoriaModulo>()); } 组 { } }
    公共IDbSet< CategoriaMateria> CategoriaMateria {{返回_categoriaMateria? (_categoriaMateria =新FakeDbSet&其中; CategoriaMateria>()); } 组 { } }
    公共IDbSet<网站>网站{{返回_SITE? (_site =新FakeDbSet<网站>()); } 组 { } }
    公共IDbSet<模>模{{返回_modulo? (_modulo =新FakeDbSet<模>()); } 组 { } }
    公共IDbSet< PERFIL> PERFIL {{返回_perfil? (_perfil =新FakeDbSet&其中; PERFIL>()); } 组 { } }
    公共IDbSet< CategoriaGaleriaImagem> CategoriaGaleriaImagem {{返回_categoriaGaleriaImagem? (_categoriaGaleriaImagem =新FakeDbSet&其中; CategoriaGaleriaImagem>()); } 组 { } }

    公共无效的SaveChanges()
    {
        //没做什么
    }

    公共IDbSet< TEntity>设置< TEntity>(),其中TEntity:类
    {
        的foreach(在typeof运算的PropertyInfo属性(FakeDbContext).GetProperties())
        {
            如果(property.PropertyType == typeof运算(IDbSet< TEntity>))
                返回property.GetValue(这一点,NULL)作为IDbSet< TEntity取代;
        }
        抛出新的异常(类型集合中找不到);
    }

    公共System.Data.Entity.Infrastructure.DbEntityEntry进入< TEntity>(TEntity实体),其中TEntity:类
    {
    }
}
 

我不能够实现,最后一种方法你们可以帮助我吗?

我用这种输入方法在我的基础信息库更新实体:

 公共抽象类BaseRepository< TEntity> :IBaseRepository< TEntity>其中,TEntity:类
    {
        #地区的字段

        保护TEntity的EntityType;
        保护IDbSet< TEntity> DbSet;

        #endregion

        #区域属性

        公共IDbContext的DbContext
        {
            得到
            {
                返回DbContextFactory.Instance.GetOrCreateContext();
            }
        }

        #endregion

        #区域构造

        保护BaseRepository()
        {
            this.EntityType = DependencyResolverFactory.Instance.Get&其中; TEntity>();
            this.DbSet = DbContext.Set&其中; TEntity>();
        }

        #endregion

        #地区方法

        公共虚拟无效添加(TEntity实体)
        {
            this.DbSet.Add(实体);
        }

        公共虚拟无效删除(TEntity实体)
        {
            this.DbSet.Remove(实体);
        }

        公共虚拟无效RemoveById(对象ID)
        {
            TEntity实体= this.GetById(ID);
            this.DbSet.Remove(实体);
        }

        公共虚拟无效编辑(TEntity实体)
        {
            this.DbContext.Entry(实体).State = EntityState.Modified;
        }

        公共虚拟TEntity GetById(对象ID)
        {
            返程(TEntity)已this.DbSet.Find(ID);
        }

        公共虚拟IList的< TEntity> GETALL()
        {
            返程((IEnumerable的< TEntity>)this.DbSet).ToList();
        }

        #endregion
    }
 
如何搭建软件自动化测试框架

解决方案

读this而你继续之前链接的问题。测试什么返回EF相关的类或LINQ到实体的工作单位是危险的。

放弃与单元测试你的存储库,并通过伪造资料库本身,而不是单元测试你的应用程序逻辑。如果你想测试你的存储库创建集成测试谈话的真正的数据库。

I'm have a Base Repository and all Entities repositories inherits from that.

In my testes i create a Fake DbContext and Fake DbSet to test my repositories, but when implementing some methods in my FakeDbContext I'm not able to implement the IDbContext.Entry method:

public class FakeDbContext : IDbContext
{
    private IDbSet<Usuario> _usuario;
    private IDbSet<Atividade> _atividade;
    private IDbSet<Autor> _autor;
    private IDbSet<CategoriaModulo> _categoriaModulo;
    private IDbSet<CategoriaMateria> _categoriaMateria;
    private IDbSet<Site> _site;
    private IDbSet<Modulo> _modulo;
    private IDbSet<Perfil> _perfil;
    private IDbSet<CategoriaGaleriaImagem> _categoriaGaleriaImagem;

    public IDbSet<Usuario> Usuario { get { return _usuario ?? (_usuario = new FakeDbSet<Usuario>()); } set { } }
    public IDbSet<Atividade> Atividade { get { return _atividade ?? (_atividade = new FakeDbSet<Atividade>()); } set { } }
    public IDbSet<Autor> Autor { get { return _autor ?? (_autor = new FakeDbSet<Autor>()); } set { } }
    public IDbSet<CategoriaModulo> CategoriaModulo { get { return _categoriaModulo ?? (_categoriaModulo = new FakeDbSet<CategoriaModulo>()); } set { } }
    public IDbSet<CategoriaMateria> CategoriaMateria { get { return _categoriaMateria ?? (_categoriaMateria = new FakeDbSet<CategoriaMateria>()); } set { } }
    public IDbSet<Site> Site { get { return _site ?? (_site = new FakeDbSet<Site>()); } set { } }
    public IDbSet<Modulo> Modulo { get { return _modulo ?? (_modulo = new FakeDbSet<Modulo>()); } set { } }
    public IDbSet<Perfil> Perfil { get { return _perfil ?? (_perfil = new FakeDbSet<Perfil>()); } set { } }
    public IDbSet<CategoriaGaleriaImagem> CategoriaGaleriaImagem { get { return _categoriaGaleriaImagem ?? (_categoriaGaleriaImagem = new FakeDbSet<CategoriaGaleriaImagem>()); } set { } }

    public void SaveChanges()
    {
        //do nothing
    }

    public IDbSet<TEntity> Set<TEntity>() where TEntity : class
    {
        foreach (PropertyInfo property in typeof(FakeDbContext).GetProperties())
        {
            if (property.PropertyType == typeof(IDbSet<TEntity>))
                return property.GetValue(this, null) as IDbSet<TEntity>;
        }
        throw new Exception("Type collection not found");
    }

    public System.Data.Entity.Infrastructure.DbEntityEntry Entry<TEntity>(TEntity entity) where TEntity : class
    {
    }
}

The last method I'm not able to implementing, can you guys help me?

I'm using this Entry method to update a Entity in my base repository:

public abstract class BaseRepository<TEntity> : IBaseRepository<TEntity> where TEntity : class
    {
        #region Fields

        protected TEntity EntityType;
        protected IDbSet<TEntity> DbSet;

        #endregion

        #region Properties

        public IDbContext DbContext
        {
            get
            {
                return DbContextFactory.Instance.GetOrCreateContext();
            }
        }

        #endregion

        #region Constructors

        protected BaseRepository()
        {
            this.EntityType = DependencyResolverFactory.Instance.Get<TEntity>();
            this.DbSet = DbContext.Set<TEntity>();
        }

        #endregion

        #region Methods

        public virtual void Add(TEntity entity)
        {
            this.DbSet.Add(entity);
        }

        public virtual void Remove(TEntity entity)
        {
            this.DbSet.Remove(entity);
        }

        public virtual void RemoveById(object id)
        {
            TEntity entity = this.GetById(id);
            this.DbSet.Remove(entity);
        }

        public virtual void Edit(TEntity entity)
        {
            this.DbContext.Entry(entity).State = EntityState.Modified;
        }

        public virtual TEntity GetById(object id)
        {
            return (TEntity)this.DbSet.Find(id);
        }

        public virtual IList<TEntity> GetAll()
        {
            return ((IEnumerable<TEntity>)this.DbSet).ToList();
        }

        #endregion
    }

解决方案

Read this and all linked questions before you continue. Unit testing anything returning EF related classes or working with linq-to-entities is dangerous.

Give up with unit testing your repositories and instead unit test your application logic by faking repositories themselves. If you want to test your repositories create integration tests talking to the real database.