To test my repository, I created a fake Entity Framework 4.1 DbContext.

.net c# entity-framework repository unit-testing

Question

All Entities Repositories derive from a Base Repository that I have.

To test my repositories, I construct a fake DbContext and fake DbSet in my tests, however when I try to implement specific methods in my fake DbContext, I'm unable to do so:

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

Can you guys assist me with the last approach I'm having trouble putting into practice?

I'm updating an Entity in my base repository using this Entry method:

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
    }
1
3
8/18/2011 11:52:00 AM

Accepted Answer

Before moving on, read this and all connected questions. It is risky to unit test anything that returns EF-related classes or deal with linq-to-entities.

Instead of unit testing your repositories, give up and unit test the functionality of your application by making up fake repositories. Create integration tests communicating to the actual database to test your repositories.

6
5/23/2017 11:51:00 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow