如何对我重写的DbContext.SaveChanges()进行单元测试?

时间:2016-04-07 14:45:16

标签: c# entity-framework unit-testing entity-framework-6 nunit

我在调用SaveChanges()后覆盖了base.SaveChanges()方法来调用存储过程。存储过程存放在我可以轻松模拟的存储库中,但是如何防止DbContext实际将实体保存到数据库?

更新 我还要提一下,我将这两个动作包装在这样的事务中:

public partial class MyDbContext
{
    private readonly IProcRepository _procRepository;

    public MyDbContext(IProcRepository procRepository, string connectionString) : base(connectionString)
    {
        _procRepository = procRepository;
    }

    public override int SaveChanges()
    {
        var stateEntries = 0;

        using (var dbContextTransaction = Database.BeginTransaction())
        {
            try
            {
                stateEntries = base.SaveChanges();
                _procRepository.MyStoredProcedure();
                dbContextTransaction.Commit();
            }
            catch
            {
                dbContextTransaction.Rollback();
                throw;
            }
        }
        return stateEntries;
    }
}

0 个答案:

没有答案