Пример #1
0
        public void Get_CanGetWritableEntity_WhenEntityExists()
        {
            using (var context = new FakeDbContext())
            {
                var repository = new EntityFrameworkRepository <FakeCustomer>(() => context);
                var customer   = repository.Get(1);

                Assert.NotNull(customer);
                Assert.Equal(EntityState.Unchanged, context.Entry(customer).State);
            }
        }
Пример #2
0
        public void Query_CanGetReadOnlyEntity_WhichAreNotTrackedByContext()
        {
            using (var context = new FakeDbContext())
            {
                var repository    = new EntityFrameworkRepository <FakeCustomer>(() => context);
                var firstCustomer = repository.Query().First();
                var isFirstCustomerTrackedByContext = context.Entry(firstCustomer).State != EntityState.Detached;

                Assert.NotNull(firstCustomer);
                Assert.Equal(false, isFirstCustomerTrackedByContext);
            }
        }
Пример #3
0
        public void Delete_CanMarkEntityForDeletion_WhenEntityExists()
        {
            using (var context = new FakeDbContext())
            {
                var repository = new EntityFrameworkRepository <FakeCustomer>(() => context);
                var customer   = repository.Get(1);
                Assert.NotNull(customer);

                repository.Delete(customer);

                Assert.Equal(EntityState.Deleted, context.Entry(customer).State);
            }
        }
Пример #4
0
        public void Save_CanCreateEntity_WhenEntityIsNotTrackedByContext()
        {
            using (var context = new FakeDbContext())
            {
                var repository = new EntityFrameworkRepository <FakeCustomer>(() => context);
                var customer   = new FakeCustomer
                {
                    FirstName = "John",
                    LastName  = "Doe"
                };

                repository.Save(customer);

                Assert.Equal(EntityState.Added, context.Entry(customer).State);
            }
        }
        public void Delete_ExistingDetachedEntity_Deleted()
        {
            // Arrange
            const int id = 40;

            var dbContextOptionsBuilder = new DbContextOptionsBuilder <DbContext>().UseInMemoryDatabase(nameof(Delete_ExistingEntity_Deleted));
            var fakeDbContext           = new FakeDbContext(dbContextOptionsBuilder.Options, "dbo");
            var entity = fakeDbContext.Add(new FakeEntity {
                Id = id
            }).Entity;

            fakeDbContext.SaveChanges();

            fakeDbContext.Entry(entity).State = EntityState.Detached;

            var repository = new Repository <FakeEntity, int>(fakeDbContext);

            // Act
            repository.Delete(entity);
            fakeDbContext.SaveChanges();

            // Assert
            Assert.False(fakeDbContext.Set <FakeEntity>().Any(x => x.Id == id));
        }