Пример #1
0
        public void Can_add_product()
        {
            List<Product> products;

            using (var context = new EfDbContext())
            {
                context.Products.Add(new Product
                {
                    Name = "Name1",
                    Category = "Category1",
                    Description = "Description1",
                    Price = new decimal(2.99)
                });

                context.SaveChanges();

                products = context.Products.ToList();
            }

            products.ShouldNotBeEmpty();

            products.Count.ShouldBe(1);

            var product = products.First();

            product.Name.ShouldBe("Name1");
            product.Category.ShouldBe("Category1");
            product.Description.ShouldBe("Description1");
            product.Price.ShouldBe(2.99m);
        }
Пример #2
0
        public void Can_rollback_transaction()
        {
            using (var context = new EfDbContext())
            {
                context.Database.BeginTransaction();

                AddProductToDb(context);

                List<Product> productResults = context.Products.ToList();

                productResults.ShouldNotBeEmpty();

                productResults.Count.ShouldBe(1);

                var product = productResults.First();

                product.Name.ShouldBe("NameX");
                product.Category.ShouldBe("CategoryX");
                product.Description.ShouldBe("DescriptionX");
                product.Price.ShouldBe(4.78m);

                context.Database.CurrentTransaction.Rollback();

                List<Product> productsRolledBack = context.Products.ToList();

                productsRolledBack.ShouldBeEmpty();
            }
        }
Пример #3
0
        public void After_each_test()
        {
            DeleteAllProducts();

            using (var context = new EfDbContext())
            {
                List<Product> noProducts = context.Products.ToList();

                noProducts.ShouldBeEmpty();
            }
        }
Пример #4
0
        protected static void DeleteAllProducts()
        {
            using (var context = new EfDbContext())
            {
                List<Product> products = context.Products.ToList();

                context.Products.RemoveRange(products);

                context.SaveChanges();
            }
        }
Пример #5
0
        private void AddProducts()
        {
            List<Product> products = new List<Product>
            {
                new Product { Name = "Soccer Ball", Category = "Soccer", Description = "DescriptionX", Price = 4.78m },
                new Product { Name = "Soccer Shoes", Category = "Soccer", Description = "DescriptionX", Price = 9.99m },
                new Product { Name = "Swim Goggles", Category = "Swimming", Description = "DescriptionX", Price = 23.10m },
                new Product { Name = "Soccer Cleats", Category = "Soccer", Description = "DescriptionX", Price = 12.14m },
                new Product { Name = "Swim Cap", Category = "Swimming", Description = "DescriptionX", Price = 382.89m },
                new Product { Name = "Swim Trunks", Category = "Swimming", Description = "DescriptionX", Price = 29.99m },
                new Product { Name = "Putter", Category = "Golf", Description = "DescriptionX", Price = 48.64m },
                new Product { Name = "Driver", Category = "Golf", Description = "DescriptionX", Price = 99.00m },
                new Product { Name = "Pitching Wedge", Category = "Golf", Description = "DescriptionX", Price = 72.29m },
                new Product { Name = "Dozen Golf Balls", Category = "Golf", Description = "DescriptionX", Price = 24.99m },
                new Product { Name = "Deluxe Golf Bag", Category = "Golf", Description = "DescriptionX", Price = 89.88m },
                new Product { Name = "V8 Turbo Charged Golf Cart", Category = "Golf", Description = "DescriptionX", Price = 14682.29m },
            };

            using (var context = new EfDbContext())
            {
                context.Products.AddRange(products);

                context.SaveChanges();
            }
        }
Пример #6
0
        public void Can_roll_back_implicitly()
        {
            // Test scenario:
            // instantiate a good entity
            // then instantiate an entity not mapped to any table
            // call SaveChanges() and we should find that the good entity did not save.

            List<Product> products;

            using (var context = new EfDbContext())
            {
                context.Products.Add(new Product
                {
                    Name = "Booger",
                    Category = "Category1",
                    Description = "Description1",
                    Price = new decimal(2.99)
                });

                context.SaveChanges();

                products = context.Products.ToList();
            }
        }
Пример #7
0
        private static void AddProductToDb(EfDbContext context)
        {
            context.Products.Add(new Product
            {
                Name = "NameX",
                Category = "CategoryX",
                Description = "DescriptionX",
                Price = 4.78m
            });

            context.SaveChanges();
        }