Exemple #1
0
        public void TestValidGetById()
        {
            using (var connection = Effort.DbConnectionFactory.CreateTransient())
            {
                // arrange
                var dbContext = new CarAdvertDbContext(connection, false);
                var carAdvert = new CarAdvert()
                {
                    Title = "Audi",
                    Price = 1500,
                    IsNew = true
                };
                dbContext.CarAdverts.Add(carAdvert);
                dbContext.SaveChanges();

                var unitOfWork = Substitute.For <IUnitOfWork>();
                unitOfWork.ProvideContext <CarAdvertDbContext>().Returns(p => dbContext);
                var repository = new CarAdvertRepository(unitOfWork);

                // act
                var item = repository.Get(carAdvert.Id);

                // assert
                Assert.AreEqual(item.Title, carAdvert.Title);
                Assert.AreEqual(item.Price, carAdvert.Price);
                Assert.AreEqual(item.IsNew, carAdvert.IsNew);
            }
        }
Exemple #2
0
        public void TestValidCreate()
        {
            using (var connection = Effort.DbConnectionFactory.CreateTransient())
            {
                // arrange
                var dbContext = new CarAdvertDbContext(connection, false);

                var dbContextProvider = Substitute.For <IDbContextProvider>();
                dbContextProvider.Provide().Returns(p => dbContext);

                using (var unitOfWork = new DbContextUnitOfWork(dbContextProvider))
                {
                    var carAdvert = new CarAdvert()
                    {
                        Title = "Audi",
                        Price = 1500,
                        IsNew = true
                    };

                    var repository = new CarAdvertRepository(unitOfWork);

                    // act
                    repository.Create(carAdvert);
                    unitOfWork.Commit();

                    // assert
                    var item = dbContext.CarAdverts.Single();
                    Assert.AreEqual(item.Title, carAdvert.Title);
                    Assert.AreEqual(item.Price, carAdvert.Price);
                    Assert.AreEqual(item.IsNew, carAdvert.IsNew);
                }
            }
        }
Exemple #3
0
        public void TestValidDelete()
        {
            using (var connection = Effort.DbConnectionFactory.CreateTransient())
            {
                // arrange
                var dbContext = new CarAdvertDbContext(connection, false);
                var carAdvert = new CarAdvert()
                {
                    Title = "Audi",
                    Price = 1500,
                    IsNew = true
                };
                dbContext.CarAdverts.Add(carAdvert);
                dbContext.SaveChanges();

                var dbContextProvider = Substitute.For <IDbContextProvider>();
                dbContextProvider.Provide().Returns(p => dbContext);

                using (var unitOfWork = new DbContextUnitOfWork(dbContextProvider))
                {
                    var repository        = new CarAdvertRepository(unitOfWork);
                    var carAdvertToDelete = repository.Get(carAdvert.Id);

                    // act
                    repository.Delete(carAdvertToDelete);
                    unitOfWork.Commit();

                    // assert
                    var isAny = dbContext.CarAdverts.Any();
                    Assert.IsFalse(isAny);
                }
            }
        }