C# 如何添加使用工作单元和存储库模式的简单函数
我的测试是这样的C# 如何添加使用工作单元和存储库模式的简单函数,c#,unit-testing,moq,C#,Unit Testing,Moq,我的测试是这样的 [Fact] public void SimpleAddTest() { // Arrange var authorizationsToBeAdded = new List<PatientPayerAuthInfo> { new PatientPayerAuthInfo (), new PatientPayerAuthInfo ()
[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();
}
然后有一个commit方法调用EF的SaveChanges
我的问题是如何使用persistentAuthorizations设置mockedUnitOfWork,这样当我使用AuthorizationsTobeaded添加两个对象时,persistentAuthorizations的总数将变为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();
}
公共接口i假设,其中T:class
{
无效添加(T实体);
}
公共接口IUOW,其中U:DbContext,IDisposable
{
IRepository PatientPayerAuthInfos{get;}
void SaveChanges();
}
使用列表作为持续授权的基础,例如:
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();
当我们试图设置PatientPayerAuthInfos属性时,它属于IRepository类型,并且我们正在使用数据进行设置。这是行不通的。除此之外,PatientPayerAuthInfos和所有其他此类属性没有任何设置器。IRepository PatientPayerAuthInfos{get;}那么
iOW
是从IRepository
派生出来的吗?
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();
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);