c# - 如何最小起订量使用工作单元和存储库模式的简单添加功能

标签 c# unit-testing moq

我的测试看起来像这样

        [Fact]
    public void SimpleAddTest()
    {
        // Arrange
        var authorizationsToBeAdded = new List<PatientPayerAuthInfo>
        {
            new PatientPayerAuthInfo (),
            new PatientPayerAuthInfo  ()
        }.ToList();
        var persistentAuthorizations = new List<PatientPayerAuthInfo>
        {
            new PatientPayerAuthInfo {PatientPayerAuthInfoId = 1 },
            new PatientPayerAuthInfo  {PatientPayerAuthInfoId = 2 },
             new PatientPayerAuthInfo  {PatientPayerAuthInfoId = 3 },
             new PatientPayerAuthInfo  {PatientPayerAuthInfoId = 4 }
        }.AsQueryable();

        var mockSet = new Mock<DbSet<PatientPayerAuthInfo>>();
        mockSet.As<IQueryable<PatientPayerAuthInfo>>().Setup(m => m.Provider).Returns(persistentAuthorizations.Provider);
        mockSet.As<IQueryable<PatientPayerAuthInfo>>().Setup(m => m.Expression).Returns(persistentAuthorizations.Expression);
        mockSet.As<IQueryable<PatientPayerAuthInfo>>().Setup(m => m.ElementType).Returns(persistentAuthorizations.ElementType);
        mockSet.As<IQueryable<PatientPayerAuthInfo>>().Setup(m => m.GetEnumerator()).Returns(persistentAuthorizations.GetEnumerator());

        var mockedUnitOfWork = new Mock<IUOW<DBContext>>();     

        var service = new PatientPayerService(mockedUnitOfWork.Object);

        // Act
        var sut = service.AddPatientPayerAuthInfos(authorizationsToBeAdded);

        // Assert

    }

服务层函数如下所示

        public void AddPatientPayerAuthInfos(IEnumerable<PatientPayerAuthInfo> patientPayerAuthInfos)
    {
        foreach (var patientPayerAuthInfo in patientPayerAuthInfos)
        {
            UOW.PatientPayerAuthInfos.Add(patientPayerAuthInfo);
        }
        UOW.SaveChanges();
    }

并且存储库实现是

     public virtual void Add(T entity)
    {
        DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
        if (dbEntityEntry.State != EntityState.Detached)
        {
            dbEntityEntry.State = EntityState.Added;
        }
        else
        {
            DbSet.Add(entity);
        }
    }

然后它有一个调用 EF 的 SaveChanges 的提交方法。

我的问题是我们如何使用permanentAuthorizations设置mockedUnitOfWork,以便当我使用authorizationsToBeAdded添加两个对象时,permanentAuthorizations的总数变为6,最初是4。

如果我走错了路,请纠正我。

  public interface IRepository<T> where T : class
  {
    void Add(T entity);
   }

public interface IUOW<U> where U : DbContext, IDisposable
{        
    IRepository<PatientPayerAuthInfo> PatientPayerAuthInfos { get; }
    void SaveChanges();
}

最佳答案

使用列表作为persistentAuthorizations的基础,例如:

var data = new List<PatientPayerAuthInfo>
{
    new PatientPayerAuthInfo {PatientPayerAuthInfoId = 1 },
    new PatientPayerAuthInfo {PatientPayerAuthInfoId = 2 },
    new PatientPayerAuthInfo {PatientPayerAuthInfoId = 3 },
    new PatientPayerAuthInfo {PatientPayerAuthInfoId = 4 }
};
var persistentAuthorizations = data.AsQueryable();

然后你可以像这样设置mockedUnitOfWork:

var repositoy = new Mock<IRepository<PatientPayerAuthInfo>>();

// when adding data to the repository, add the item to 'data' 
repositoy.Setup(r => r.Add(It.IsAny<PatientPayerAuthInfo>()))
         .Callback(delegate(PatientPayerAuthInfo y)
                   {
                        data.Add(y);
                   });

// when accessing 'PatientPayerAuthInfos', use the repository mock
var mockedUnitOfWork = new Mock<IUOW<DBContext>>();
mockedUnitOfWork.SetupGet(x => x.PatientPayerAuthInfos).Returns(() => repositoy.Object);

关于c# - 如何最小起订量使用工作单元和存储库模式的简单添加功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28154956/

相关文章:

c# - 如何为存储库设置最小起订量

c# - 如何在 Visual Studio 测试中模拟数据存储库?

c# - HiddenFor 应该尊重 DisplayFormat 吗?

visual-studio - 发布数据库项目作为 TestInitialize 的一部分

unit-testing - grails spock测试模拟CommonsMultipartFile

c++ - 如何在 cppunit 中断言语句抛出 Excp1 或 Excp2 类型的异常?

c# - 如何对 session 值进行单元测试?

c# - Linq2SQL,异常后使用dataContext

c# - 在 asp.net mvc 应用程序中对多个复选框使用属性验证

c# - 计算2次之间的差值,然后比较差值是否小于5分钟