how to moq simple add function that uses Unit of Work and Repository Pattern

喜你入骨 提交于 2019-12-06 07:56:03

Use a list as base for persistentAuthorizations like:

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();

Then you can setup mockedUnitOfWork like this:

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!