public void Option1_TestDiscount_Success()
        {
            try
            {
                _connection.Open();
                var options = DatabaseHelper.GetContextOptions(_connection);
                // Add products and discounts to Test DB
                DatabaseHelper.SetupProducts(_connection);

                using (var context = new AppDbContext(options))
                {
                    var repo              = new SupermarketRepository(context);
                    var controller        = new BasketController(repo);
                    var expectedTotal     = 2.95m;
                    var expectedLineCount = 3;

                    // Add 1 bread, 1 milk and 1 butter
                    controller.AddToBasket(1);
                    controller.AddToBasket(2);
                    controller.AddToBasket(3);

                    var model = (controller.Index(_basketId) as ViewResult).Model as BasketViewModel;
                    Assert.True(model.ProductLineItems.Count == expectedLineCount);
                    Assert.True(model.Total == expectedTotal); // Basket total before discount

                    var updatedViewModel = (controller.UpdateTotal(_basketId) as ViewResult).Model as BasketViewModel;
                    Assert.True(updatedViewModel.Total == expectedTotal); // Basket Total after update
                }
            }
            finally
            {
                _connection.Close();
            }
        }
Esempio n. 2
0
        public void HomeController_ProductsView_Success()
        {
            try
            {
                _connection.Open();
                var options = DatabaseHelper.GetContextOptions(_connection);

                using (var context = new AppDbContext(options))
                {
                    context.Database.EnsureCreated();
                }

                // Add products to Test DB
                DatabaseHelper.SetupProducts(_connection);

                using (var context = new AppDbContext(options))
                {
                    var repo       = new SupermarketRepository(context);
                    var controller = new HomeController(repo);
                    var result     = controller.Index() as ViewResult;

                    Assert.NotNull(result);
                    HomeViewModel model = result.Model as HomeViewModel;
                    Assert.True(model.BasketId == 1);
                    Assert.True(model.Products.Count == 3);
                }
            }
            finally
            {
                _connection.Close();
            }
        }
        public void BasketController_AddToBasket_Test_Success()
        {
            try
            {
                _connection.Open();
                var options = DatabaseHelper.GetContextOptions(_connection);
                // Add products to Test DB
                DatabaseHelper.SetupProducts(_connection);

                using (var context = new AppDbContext(options))
                {
                    var repo       = new SupermarketRepository(context);
                    var controller = new BasketController(repo);

                    // Add 4 containers of milk to the basket. 1 line item
                    controller.AddToBasket(2);
                    controller.AddToBasket(2);
                    controller.AddToBasket(2);
                    controller.AddToBasket(2);

                    var result = controller.Index(_basketId) as ViewResult;
                    Assert.NotNull(result);
                    var model = result.Model as BasketViewModel;
                    Assert.True(model.BasketId == 1);
                    Assert.True(model.ProductLineItems.Count == 1);

                    // Basket total before discount
                    Assert.True(model.Total == 4.60m);

                    var updatedresult    = controller.UpdateTotal(_basketId) as ViewResult;
                    var updatedViewModel = updatedresult.Model as BasketViewModel;
                    Assert.True(updatedViewModel.Total == 3.45m);
                }
            }
            finally
            {
                _connection.Close();
            }
        }