伪造 Entity Framework 4.1 的 DbContext 来测试我的存储库

发布于 2024-11-30 08:53:12 字数 4174 浏览 0 评论 0原文

我有一个基本存储库,所有实体存储库都继承自该存储库。

在我的睾丸中,我创建了一个 Fake DbContext 和 Fake DbSet 来测试我的存储库,但是当在 FakeDbContext 中实现某些方法时,我无法实现 IDbContext.Entry 方法:

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
    {
    }
}

我的最后一个方法无法实施,你们能帮我吗?

我正在使用此 Entry 方法来更新我的基础存储库中的实体:

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
    }

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
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迟月 2024-12-07 08:53:12

阅读此内容以及所有链接的问题在您继续之前。对任何返回 EF 相关类或使用 linq-to-entities 进行单元测试都是危险的。

放弃对存储库进行单元测试,而是通过伪造存储库本身来对应用程序逻辑进行单元测试。如果您想测试您的存储库,请创建与真实数据库对话的集成测试。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文