开发者

EF 4.1 :: Testing with Moq & NUnit

开发者 https://www.devze.com 2023-03-27 15:44 出处:网络
I am using Entity Framework 4.1 for my DAL on my current project, and am now trying to unit test my business objects while mocking my entities with moq.

I am using Entity Framework 4.1 for my DAL on my current project, and am now trying to unit test my business objects while mocking my entities with moq.

I have created a generic Unit of Work

public interface IFRSDbContext
{
    IDbSet<Category> Categories { get; set; }
    IDbSet<Cell> Cells { get; set; }
    IDbSet<DealSummary> DealSummaries { get; set; }
    IDbSet<DealSummaryDetail> DealSummaryDetails { get; set; }
    IDbSet<Node> Nodes { get; set; }
    IDbSet<Rto> Rtos { get; set; }
    IDbSet<Sheet> Sheets { get; set; }
    IDbSet<Version> Versions { get; set; }
    IDbSet<VersionMapping> VersionMappings { get; set; }

    DbEntityEntry Entry(object entity); 
    DbSet<TEntity> Set<TEntity>() where TEntity : class;
    int SaveChanges();
}

As well as a generic Repository

public abstract class Repository<TEntity> where TEntity : class
{
    protected IFRSDbContext DbContext;

    protected Repository(IFRSDbContext context) 
    { 
        DbContext = context;
    } 


    public virtual TEntity GetById(object id)
    {
        return DbContext.Set<TEntity>().Find(id);
    }


    public virtual void Insert(TEntity entity)
    {
        DbContext.Set<TEntity>().Add(entity);
    }


    public virtual void Delete(object id)
    { 
        var entityToDelete = DbContext.Set<TEntity>().Find(id); 
        Delete(entityToDelete); 
    } 


    public virtual void Delete(TEntity entityToDelete)
    {
        DbContext.Set<TEntity>().Remove(entityToDelete);
    }


    public abstract void Update(TEntity entityToUpdate);
}

I also have a repository for each entity, here is an example:

public class DealSummaryRepository : Repository<DealSummary>
{
    public DealSummaryRepository(IFRSDbContext context) : base(context) { }


    public virtual DealSummary GetByFileName(string fileName)
    {
        return DbContext.Set<DealSummary>().FirstOrDefault(d => d.FileName == fileName);
    }


    public override void Update(DealSummary entityToUpdate)
    {
        var existingDealSummary = GetByFileName(entityToUpdate.FileName);

        if (existingDealSummary == null)
        {
            var message = string.Format(@"Error :: Cannot update Deal Summary '{0}' because it does not exist
                                        in the database.", entityToUpdate.FileName);

            throw new Exception(message);
        }

        existingDealSummary.DateModified = DateTime.Now;
        existingDealSummary.StartDate = entityToUpdate.StartDate;
        existingDealSummary.EndDate = entityToUpdate.EndDate;
        existingDealSummary.DueDate = entityToUpdate.DueDate;
        existingDealSummary.WasWon = entityToUpdate.WasWon;
        existingDealSummary.UploadedBy = entityToUpdate.UploadedBy;

        if (existingDealSummary.Details != null)
            existingDealSummary.Details.Clear();

        existingDealSummary.Details = entityToUpdate.Details;
    }
}

The question that I have is, is there a way to implement the IDbSet object as my generic repository and inherit that... or should I contain my repositories in开发者_Python百科 my unit of work, and implement the IDbSet inside of the repositories? The only problem I have with implementing the IDbSet inside of the repository is then my repository is dependant on EF.

Any sugguestions/best practices would be greatly appreciated. I am trying to take the simplest approach possible to make my entities mockable so I can test without the dependency to entity framework/my database.


I've been using the EF Code First + Repositories pattern from http://efmvc.codeplex.com which has a couple differences from the way you've constructed yours

First thing I noticed is your Unit of Work is tied to EF. EFMVC's Unit of Work is simply

public interface IUnitOfWork
{
    void Commit();
}

To avoid tying the repositories to EF, we have

public interface IRepository<T> where T : class
{
    void Add(T entity);
    void Update(T entity);
    void Delete(T entity);
    void Delete(Expression<Func<T, bool>> where);
    T GetById(long Id);
    T GetById(string Id);
    T Get(Expression<Func<T, bool>> where);
    IQueryable<T> GetAll();
    IQueryable<T> GetMany(Expression<Func<T, bool>> where);
}

and our implementation of IRepository<T> is what requires the dependency on EF. So now instead of mocking IFRSDbContext with its IDbSets (EntityFramework), you mock an IRepository<T> with its IQueryables (System.Core)

EDIT: To your question, it might look something like this

public class Uploader : IUploader
{ 
    private readonly IReportRepository _reportRepository; 
    private readonly IUnitOfWork _unitOfWork; 

    public Uploader(IReportRepository reportRepository, IUnitOfWork unitOfWork) 
    { 
        _reportRepository = reportRepository;
        _unitOfWork = unitOfWork;
    }

    public void Upload(Report report)
    {
        _reportRepository.Add(report);
        _unitOfWork.Commit();
    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消