mercredi 28 janvier 2015

for unit testing (moq) how to set up repository which takes DbContext as a constructor.

my repository implementation looks something like this



public class EFRepository<T> : IRepository<T> where T : class
{
public EFRepository(DbContext dbContext)
{
if (dbContext == null)
throw new ArgumentNullException("dbContext");
DbContext = dbContext;
DbSet = DbContext.Set<T>();
}

protected DbContext DbContext { get; set; }

protected DbSet<T> DbSet { get; set; } }


my service layer function looks like this



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


i am writing the unit test for AddPatientPayerAuthInfos . when i try to do



var mockedUnitOfWork = new Mock<IUOW<MyDBContext>>();
mockedUnitOfWork.SetupGet(p => p.PatientPayerAuthInfos).Returns(what should i do here ?);


PatientPayerAuthInfos is of type IRepository


i tried to do



private object GetMockPPAIRepository()
{
var value = new EFRepository<PatientPayerAuthInfo>(what should i pass here);
value.Add(new PatientPayerAuthInfo { PatientPayerAuthInfoId = 1 });
value.Add(new PatientPayerAuthInfo { PatientPayerAuthInfoId = 2 });
value.Add(new PatientPayerAuthInfo { PatientPayerAuthInfoId = 3 });
return value;

}


where EFRepository implements IRepository.


any idea ?


Aucun commentaire:

Enregistrer un commentaire