2

My test looks like this

        [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

    }

service layer function looks like this

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

AND Repository implementation is

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

which then has a commit method that calls EF's SaveChanges.

my question is how we can Setup mockedUnitOfWork using persistentAuthorizations so that when i add Two objects using authorizationsToBeAdded then the total count of persistentAuthorizations becomes 6, which are 4 initially.

or correct me if i am on wrong track.

  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();
}
sloth
  • 99,095
  • 21
  • 171
  • 219
Raas Masood
  • 1,475
  • 3
  • 23
  • 61

1 Answers1

2

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);
sloth
  • 99,095
  • 21
  • 171
  • 219
  • when we are trying to setup property PatientPayerAuthInfos it is of type IRepository and we are setting it up with data. which is not working. other than that PatientPayerAuthInfos and all other such properties does not have any setters. IRepository PatientPayerAuthInfos { get; } – Raas Masood Jan 27 '15 at 17:04
  • So `IUOW` is derived from `IRepository`? – sloth Jan 28 '15 at 15:52