Exemplo n.º 1
0
 public void TestGetPermissableEntities_AddedEntityState()
 {
     using (ShimsContext.Create())
     {
         var entity = new PermissableEntity
         {
             Id = 1,
             PermissableType = PermissableType.Project
         };
         System.Data.Entity.Infrastructure.Fakes.ShimDbChangeTracker.AllInstances.Entries = (tracker) =>
         {
             var entries = new List <DbEntityEntry>();
             entries.Add(new System.Data.Entity.Infrastructure.Fakes.ShimDbEntityEntry
             {
                 EntityGet = () => entity,
                 StateGet  = () => EntityState.Added
             });
             return(entries);
         };
         context.PermissableEntities.Add(entity);
         var entities = saveAction.GetPermissableEntities(context, EntityState.Added);
         Assert.AreEqual(1, entities.Count);
         Assert.IsTrue(Object.ReferenceEquals(entity, entities.First()));
     }
 }
Exemplo n.º 2
0
        public async Task TestBeforeSaveChanges_AddedAndModifiedEntities()
        {
            using (ShimsContext.Create())
            {
                var addedEntity = new PermissableEntity
                {
                    Id = 1,
                    PermissableType = PermissableType.Project
                };
                var updatedEntity = new PermissableEntity
                {
                    Id = 2,
                    PermissableType = PermissableType.Program
                };
                System.Data.Entity.Infrastructure.Fakes.ShimDbChangeTracker.AllInstances.Entries = (tracker) =>
                {
                    var entries = new List <DbEntityEntry>();
                    entries.Add(new System.Data.Entity.Infrastructure.Fakes.ShimDbEntityEntry
                    {
                        EntityGet = () => addedEntity,
                        StateGet  = () => EntityState.Added
                    });
                    entries.Add(new System.Data.Entity.Infrastructure.Fakes.ShimDbEntityEntry
                    {
                        EntityGet = () => updatedEntity,
                        StateGet  = () => EntityState.Modified
                    });
                    return(entries);
                };
                context.PermissableEntities.Add(addedEntity);
                context.PermissableEntities.Add(updatedEntity);
                Action <IList <IPermissable> > onAddedTester = (addedPermissableEntities) =>
                {
                    Assert.AreEqual(1, addedPermissableEntities.Count);
                    Assert.IsTrue(Object.ReferenceEquals(addedEntity, addedPermissableEntities.First()));
                };

                Action <IList <IPermissable> > onModifiedTester = (updatedPermissableEntities) =>
                {
                    Assert.AreEqual(1, updatedPermissableEntities.Count);
                    Assert.IsTrue(Object.ReferenceEquals(updatedEntity, updatedPermissableEntities.First()));
                };

                saveAction.BeforeSaveChanges(context);
                onAddedTester(saveAction.AddedEntities);
                onModifiedTester(saveAction.ModifiedEntities);
                saveAction.AddedEntities.Clear();
                saveAction.ModifiedEntities.Clear();

                await saveAction.BeforeSaveChangesAsync(context);

                onAddedTester(saveAction.AddedEntities);
                onModifiedTester(saveAction.ModifiedEntities);
                saveAction.AddedEntities.Clear();
                saveAction.ModifiedEntities.Clear();
            }
        }