public BasketControllerTests()
        {
            var logger = new Mock <ILogger>();

            _repository       = new Mock <IBasketRepository>();
            _basketController = new BasketController(_repository.Object, logger.Object);
        }
Example #2
0
        public async Task Doing_Checkout_Wit_Basket_Should_Publish_UserCheckoutAccepted_Integration_Event()
        {
            var fakeCustomerId     = "1";
            var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId);

            _basketRepositoryMock.Setup(x => x.GetBasketAsync(It.IsAny <string>()))
            .Returns(Task.FromResult(fakeCustomerBasket));

            _identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);

            var basketController = new BasketController(
                _basketRepositoryMock.Object, _identityServiceMock.Object, _serviceBusMock.Object);

            basketController.ControllerContext = new ControllerContext()
            {
                HttpContext = new DefaultHttpContext()
                {
                    User = new ClaimsPrincipal(
                        new ClaimsIdentity(new Claim[] { new Claim("unique_name", "testuser") }))
                }
            };

            //Act
            var result = await basketController.Checkout(new BasketCheckout(), Guid.NewGuid().ToString()) as AcceptedResult;

            _serviceBusMock.Verify(mock => mock.Publish(It.IsAny <UserCheckoutAcceptedIntegrationEvent>()), Times.Once);

            Assert.NotNull(result);
        }
        public void CanAddBasketItem()
        {
            //Setup Info
            IRepository <Basket>   baskets   = new MockContext <Basket>();
            IRepository <Product>  products  = new MockContext <Product>();
            IRepository <Order>    orders    = new MockContext <Order>();
            IRepository <Customer> customers = new MockContext <Customer>();

            var httpContext = new MockHttpContext();

            IBasketService basketService = new BasketService(products, baskets);
            IOrderService  orderService  = new OrderService(orders);
            var            controller    = new BasketController(basketService, orderService, customers);

            controller.ControllerContext = new System.Web.Mvc.ControllerContext(httpContext, new System.Web.Routing.RouteData(), controller);

            //Act on Info
            controller.AddToBasket("1");

            basketService.AddToBasket(httpContext, "1");

            Basket basket = baskets.Collection().FirstOrDefault();

            //Confirm on Info
            Assert.IsNotNull(basket);
            Assert.AreEqual(1, basket.BasketItems.Count);
            Assert.AreEqual("1", basket.BasketItems.ToList().FirstOrDefault().ProductId);
        }
        public void CanAddBasketItem()
        {
            //Setup
            IRepository <Basket>   baskets   = new MockRepository <Basket>();
            IRepository <Product>  products  = new MockRepository <Product>();
            IRepository <Order>    orders    = new MockRepository <Order>();
            IRepository <Customer> customers = new MockRepository <Customer>();

            IBasketService basketService = new BasketService(products, baskets);
            IOrderService  orderService  = new OrderService(orders);
            var            controller    = new BasketController(basketService, orderService, customers);

            var httpContext = new MockHttpContext();

            controller.ControllerContext = new ControllerContext(httpContext, new System.Web.Routing.RouteData(), controller);

            //Act
            //to test the service directly we would do this
            //basketService.AddToBasket(httpContext, "1");
            //to test the service via the controller we do this
            controller.AddToBasket("1");
            Basket basket = baskets.Collection().FirstOrDefault();

            //Assert
            Assert.IsNotNull(basket);
            Assert.AreEqual(1, basket.BasketItems.Count);
            Assert.AreEqual("1", basket.BasketItems.ToList().FirstOrDefault().ProductId);
        }
Example #5
0
        public void CanGetSummaryViewModel()
        {
            IRepository<Basket> baskets = new MockContext<Basket>();
            IRepository<Product> products = new MockContext<Product>();
            IRepository<Order> orders = new MockContext<Order>();
            IRepository<Customer> customers = new MockContext<Customer>();

            products.Insert(new Product() { Id = "1", Price = 10.00m });
            products.Insert(new Product() { Id = "2", Price = 5.00m });

            Basket basket = new Basket();
            basket.BasketItems.Add(new BasketItem() { ProductId = "1", Quantity = 2 });
            basket.BasketItems.Add(new BasketItem() { ProductId = "2", Quantity = 1 });
            baskets.Insert(basket);

            IBasketService basketService = new BasketService(products, baskets);
            IOrderService orderService = new OrderService(orders);
            var controller = new BasketController(basketService, orderService, customers);

            var httpContext = new MockHttpContext();
            httpContext.Request.Cookies.Add(new System.Web.HttpCookie("eCommerceBasket") { Value = basket.Id });
            controller.ControllerContext = new System.Web.Mvc.ControllerContext(httpContext, new System.Web.Routing.RouteData(), controller);

            var result = controller.BasketSummary() as PartialViewResult;
            var basketSummary = (BasketSummaryViewModel)result.ViewData.Model;

            Assert.AreEqual(3, basketSummary.BasketCount);
            Assert.AreEqual(25.00m, basketSummary.BasketTotal);
        }
 public void SetUp()
 {
     fixture           = new Fixture();
     basketMock        = new Mock <Basket>();
     basketServiceMock = new Mock <BasketService>(basketMock.Object);
     basketController  = new BasketController(basketServiceMock.Object);
 }
Example #7
0
        public void CanAddBasketItem()
        {
            IRepository <Product> productContext = new MockContext <Product>();
            //IRepository<ProductCategory> productCategoryContext = new MockContext<ProductCategory>();
            IRepository <Basket>   basketContext      = new MockContext <Basket>();
            IRepository <Customer> customerRepository = new MockContext <Customer>();


            IBasketService basketService = new BasketService(productContext, basketContext);
            IOrderService  orderService  = new OrderService(new MockContext <Order>());
            //setting up
            BasketController basketController = new BasketController(basketService, orderService, customerRepository);

            basketController.ControllerContext = new System.Web.Mvc.ControllerContext(new MockHttpContext(), new System.Web.Routing.RouteData(), basketController);
            //basketService.AddToBasket(new MockHttpContext(),"1");

            //act
            basketController.AddToBasket("1");
            var basket = basketContext.Collection().FirstOrDefault();

            //Assert
            Assert.IsNotNull(basket);
            Assert.AreEqual(1, basket.BasketItems.Count());
            Assert.AreEqual("1", basket.BasketItems.ToList().FirstOrDefault().ProductId);
        }
Example #8
0
        public void canAddBasketItems()
        {
            //setup
            IRepository <Basket>   basket    = new MockContext <Basket>();
            IRepository <Product>  product   = new MockContext <Product>();
            IRepository <Order>    orders    = new MockContext <Order>();
            IRepository <Customer> customers = new MockContext <Customer>();

            var httpcontext = new MockHttpContext();

            IBasketService basketService = new BasketService(product, basket);
            IOrderService  orderService  = new OrderService(orders);
            var            controller    = new BasketController(basketService, orderService, customers);

            controller.ControllerContext = new System.Web.Mvc.ControllerContext(httpcontext, new System.Web.Routing.RouteData(), controller);

            //Act
            //basketService.addToBasket(httpcontext, "1");
            controller.addToBasket("1");

            Basket bas = basket.collection().FirstOrDefault();

            //Assert
            Assert.IsNotNull(bas);
            Assert.AreEqual(1, bas.basketItems.Count);
            Assert.AreEqual("1", bas.basketItems.ToList().FirstOrDefault().productId);
        }
        public void CanAddBasketItem()
        {
            // Arrange
            IRepository <Basket>   baskets   = new MockContext <Basket>();
            IRepository <Product>  products  = new MockContext <Product>();
            IRepository <Order>    orders    = new MockContext <Order>();
            IRepository <Customer> customers = new MockContext <Customer>();

            var httpContext = new MockHttpContext();

            IBasketService basketService = new BasketService(products, baskets);
            IOrderService  orderService  = new OrderService(orders);

            var controller = new BasketController(basketService, orderService, customers);

            // Act
            basketService.AddToBasket(httpContext, "1");

            Basket basket = baskets.Collection().FirstOrDefault();

            // Assert
            Assert.IsNotNull(basket);
            Assert.AreEqual(1, basket.BasketItems.Count());
            Assert.AreEqual("1", basket.BasketItems.ToList().FirstOrDefault().ProductId);
        }
        public void Calculate_Specialized_Equipment_Rent_Price()
        {
            var equipmentService = new Mock <IEquipmentApiService>();
            var basketService    = new Mock <IBasketApiService>();
            var customerService  = new Mock <ICustomerApiService>();
            var cacheMemory      = new Mock <IMemoryCache>();

            BasketItemVM heavyItem = new BasketItemVM()
            {
                CustomerId = 1,
                Day        = 10,
                Equipment  = new EquipmentVM()
                {
                    Id   = 5,
                    Name = "Bosch Jackhammer",
                    Type = EquiptmentType.Specialize
                }
            };


            var result = new BasketController(equipmentService.Object, basketService.Object, customerService.Object, cacheMemory.Object)
                         .CalculateRentPrice(heavyItem);

            Assert.Equal(460, result.Price);
            Assert.Equal(1, result.Point);
        }
        public void CanAddBasketItem()
        {
            //Send in Mock repositories
            //Set Up
            IRepository <Basket>  baskets  = new MockContext <Basket>();
            IRepository <Product> products = new MockContext <Product>();

            var httpContext = new MockHttpContext();

            IBasketService basketService = new BasketService(products, baskets);
            var            controller    = new BasketController(basketService);

            controller.ControllerContext = new System.Web.Mvc.ControllerContext(httpContext, new System.Web.Routing.RouteData(), controller);



            //TestCategoryAttribute the basket service directly
            //Act
            // basketService.AddToBasket(httpContext, "1");
            controller.AddToBasket("1");

            // Looking for basket collection
            Basket basket = baskets.Collection().FirstOrDefault();



            //Test an d check
            //Assert
            Assert.IsNotNull(basket);
            Assert.AreEqual(1, basket.BasketItems.Count);
            Assert.AreEqual("1", basket.BasketItems.ToList().FirstOrDefault().ProductId);
        }
        public void Calculate_Regular_Equipment_Rent_Price()
        {
            var equipmentService = new Mock <IEquipmentApiService>();
            var basketService    = new Mock <IBasketApiService>();
            var customerService  = new Mock <ICustomerApiService>();
            var cacheMemory      = new Mock <IMemoryCache>();

            BasketItemVM heavyItem = new BasketItemVM()
            {
                CustomerId = 1,
                Day        = 10,
                Equipment  = new EquipmentVM()
                {
                    Id   = 2,
                    Name = "Kamaz Truck",
                    Type = EquiptmentType.Regular
                }
            };


            var result = new BasketController(equipmentService.Object, basketService.Object, customerService.Object, cacheMemory.Object)
                         .CalculateRentPrice(heavyItem);

            Assert.Equal(540, result.Price);
            Assert.Equal(1, result.Point);
        }
        public void Calculate_Heavy_Equipment_Rent_Price()
        {
            var equipmentService = new Mock <IEquipmentApiService>();
            var basketService    = new Mock <IBasketApiService>();
            var customerService  = new Mock <ICustomerApiService>();
            var cacheMemory      = new Mock <IMemoryCache>();

            BasketItemVM heavyItem = new BasketItemVM()
            {
                CustomerId = 1,
                Day        = 10,
                Equipment  = new EquipmentVM()
                {
                    Id   = 1,
                    Name = "Caterpillar Bulldozer",
                    Type = EquiptmentType.Heavy
                }
            };


            var result = new BasketController(equipmentService.Object, basketService.Object, customerService.Object, cacheMemory.Object)
                         .CalculateRentPrice(heavyItem);

            Assert.Equal(700, result.Price);
            Assert.Equal(2, result.Point);
        }
Example #14
0
        public async Task GetBasket_ShouldOkObject()
        {
            //Arrange
            var customer      = SetupStandardCustomer();
            var currentBasket = SetupProductsInBasket();
            var products      = SetupStandardProductsInStock();
            var fakeRepo      = SetupFakeRepo(customer, currentBasket, products);
            var mapper        = SetupMapper();
            var logger        = SetupLogger();
            var controller    = new BasketController(logger, fakeRepo, mapper);
            var customerId    = 1;

            //Act
            var result = await controller.Get(customerId);

            //Assert
            Assert.NotNull(result);
            var objResult = result as OkObjectResult;

            Assert.NotNull(objResult);
            var basketItems = objResult.Value as List <BasketItemDto>;

            Assert.NotNull(basketItems);
            Assert.True(fakeRepo.CurrentBasket.Count == basketItems.Count);
            for (int i = 0; i < fakeRepo.CurrentBasket.Count; i++)
            {
                Assert.Equal(fakeRepo.CurrentBasket[i].ProductId, currentBasket[i].ProductId);
                Assert.Equal(fakeRepo.CurrentBasket[i].ProductName, currentBasket[i].ProductName);
                Assert.Equal(fakeRepo.CurrentBasket[i].Price, currentBasket[i].Price);
                Assert.Equal(fakeRepo.CurrentBasket[i].Quantity, currentBasket[i].Quantity);
            }
        }
        public void TestBasketContrllorDetailsRedirect()
        {
            var basketController = new BasketController();
            var result           = (RedirectToRouteResult)basketController.Details(-1);

            Assert.AreEqual("Index", result.RouteValues["action"]);
        }
Example #16
0
        public BasketControllerTest()
        {
            InitializeTestData(out var order);

            var mapperMock       = new Mock <IMapper>();
            var orderServiceMock = new Mock <IOrderService>();
            var gameServiceMock  = new Mock <IGameService>();
            var httpContext      = new Mock <HttpContext>();

            httpContext.Setup(g => g.User.Identity.Name).Returns("user");
            var actionContext = new ActionContext();

            actionContext.HttpContext      = httpContext.Object;
            actionContext.ActionDescriptor = new ControllerActionDescriptor();
            actionContext.RouteData        = new RouteData();

            var controllerContext = new ControllerContext(actionContext);

            gameServiceMock.Setup(g => g.Get(It.IsAny <string>(), true)).Returns(new Game());
            orderServiceMock.Setup(o => o.GetAllCartOrder(It.IsAny <string>())).Returns(order);

            _basketController = new BasketController(orderServiceMock.Object,
                                                     mapperMock.Object,
                                                     gameServiceMock.Object);
            _basketController.ControllerContext = controllerContext;
        }
        public async Task Post_Course_Planner_Success()
        {
            // Arrange
            var fakeStudentId            = "1";
            var fakeStudentCoursePlanner = GetFakeStudentPlanner(fakeStudentId);

            _coursePlannerRepositoryMock.Setup(x => x.UpdateCoursePlannerAsync(It.IsAny <API.Models.CoursePlanner>()))
            .Returns(Task.FromResult(fakeStudentCoursePlanner));

            _identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeStudentId);

            _serviceBusMock.Setup(x => x.Publish(It.IsAny <StudentCoursePlannerCheckoutAcceptedIntegrationEvent>()));

            // Act
            var coursePlannerController = new BasketController(
                _loggerMock.Object,
                _coursePlannerRepositoryMock.Object,
                _identityServiceMock.Object,
                _serviceBusMock.Object
                );

            var actionResult = await coursePlannerController.UpdateCoursePlannerAsync(fakeStudentCoursePlanner);

            // Assert
            Assert.Equal((actionResult.Result as OkObjectResult).StatusCode, (int)HttpStatusCode.OK);
            Assert.Equal((((ObjectResult)actionResult.Result).Value as API.Models.CoursePlanner).StudentId, fakeStudentId);
        }
Example #18
0
        public void TestNoOfferBasket()
        {
            Basket         basket   = BasketController.CreateBasket("milk bread beans");
            IList <string> messages = BasketController.GetDiscountApplyedMessages(basket);

            Assert.AreEqual("(No offers available)", messages[0]);
        }
Example #19
0
        public async Task Get_customer_basket_success()
        {
            //Arrange
            var fakeCustomerId     = "1";
            var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId);

            _basketRepositoryMock.Setup(x => x.GetBasketAsync(It.IsAny <string>()))
            .Returns(Task.FromResult(fakeCustomerBasket));
            _identityServiceMock.Setup(x => x.GetUserIdentity()).Returns(fakeCustomerId);

            _serviceBusMock.Setup(x => x.Publish(It.IsAny <UserCheckoutAcceptedIntegrationEvent>()));

            //Act
            var basketController = new BasketController(
                _loggerMock.Object,
                _basketRepositoryMock.Object,
                _identityServiceMock.Object,
                _serviceBusMock.Object);

            var actionResult = await basketController.GetBasketByIdAsync(fakeCustomerId);

            //Assert
            Assert.Equal((actionResult.Result as OkObjectResult).StatusCode, (int)System.Net.HttpStatusCode.OK);
            Assert.Equal((((ObjectResult)actionResult.Result).Value as CustomerBasket).BuyerId, fakeCustomerId);
        }
Example #20
0
        public void TestApplesOfferBasket()
        {
            Basket         basket   = BasketController.CreateBasket("apples");
            IList <string> messages = BasketController.GetDiscountApplyedMessages(basket);

            Assert.AreEqual("Apples 10% off: -10p", messages[0]);
        }
        public async Task Post_ShouldReturnANewBasket()
        {
            var expectedGuid = Guid.NewGuid();

            var mediatr = new Mock <IMediator>();

            mediatr
            .Setup(m => m.Send(It.IsAny <IRequest <Basket> >(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(new Basket()
            {
                Id = expectedGuid
            }));

            var basketController = new BasketController(mediatr.Object);

            var actionResults = await basketController.Post();

            var createdResult = actionResults as CreatedAtActionResult;

            Assert.NotNull(createdResult);

            var basket = createdResult.Value as Basket;

            Assert.NotNull(basket);
            Assert.Equal(expectedGuid, basket.Id);
        }
Example #22
0
        public void CheckAddProductsToABasket()
        {
            using (var context = GetContextWithData())
                using (var controller = new BasketController(context))
                {
                    BasketRequest model = new BasketRequest();
                    model.ClientId = "A120";
                    model.Quantity = 1;
                    model.SKU      = "P113";

                    IActionResult result = controller.Add(model);
                    Assert.IsType <OkResult>(result);

                    model          = new BasketRequest();
                    model.ClientId = "A120";
                    model.Quantity = 1;
                    model.SKU      = "P113";

                    result = controller.Add(model);
                    Assert.IsType <OkResult>(result);

                    List <BasketRequest> list = (List <BasketRequest>)controller.Get("A120");
                    Assert.NotNull(list);
                    Assert.Single(list);
                    Assert.True(list[0].SKU == "P113" && list[0].TotalPrice == 280 && list[0].Quantity == 2);
                }
        }
        public void CanAddBasketItem()
        {
            // setup
            var baskets     = new MockContext <Basket>();
            var products    = new MockContext <Product>();
            var orders      = new MockContext <Order>();
            var httpContext = new MockHttpContext();
            var customers   = new MockContext <Customer>();

            var basketService = new BasketService(products, baskets);
            var orderService  = new OrderService(orders);

            customers.Insert(new Customer {
                Id = "1", Email = "*****@*****.**", ZipCode = "11000"
            });
            var fakeUser = new GenericPrincipal(new GenericIdentity("*****@*****.**", "Forms"), null);

            httpContext.User = fakeUser;
            var controller = new BasketController(basketService, orderService, customers);

            controller.ControllerContext = new ControllerContext(httpContext, new RouteData(), controller);

            // act
            //basketService.AddToBasket(httpContext, "1");
            controller.AddToBasket("1");

            var basket = baskets.Collection().FirstOrDefault();

            // assert
            Assert.IsNotNull(basket);
            Assert.AreEqual(1, basket.BasketItems.Count);
            Assert.AreEqual("1", basket.BasketItems.ToList().FirstOrDefault().ProductId);
        }
Example #24
0
        public void EmptyYourBasket()
        {
            using (var context = GetContextWithData())
                using (var controller = new BasketController(context))
                {
                    BasketRequest model = new BasketRequest();
                    model.ClientId = "A120";
                    model.Quantity = 1;
                    model.SKU      = "P113";

                    IActionResult result = controller.Add(model);
                    Assert.IsType <OkResult>(result);

                    model          = new BasketRequest();
                    model.ClientId = "A120";
                    model.Quantity = 1;
                    model.SKU      = "P113";

                    result = controller.Add(model);
                    Assert.IsType <OkResult>(result);

                    controller.EmptyBasket("A120");
                    List <BasketRequest> list = (List <BasketRequest>)controller.Get("A120");
                    Assert.True(list.Count == 0);
                }
        }
        public void CanAddBasketItem()
        {
            //setup
            IRepository <Basket>  baskets  = new MockContext <Basket>();
            IRepository <Product> products = new MockContext <Product>();

            var httpContext = new MockHttpContext();


            IBasketService basketService = new BasketService(products, baskets);
            var            controller    = new BasketController(basketService);

            controller.ControllerContext = new System.Web.Mvc.ControllerContext(httpContext, new System.Web.Routing.RouteData(), controller);

            //Act
            //basketService.AddToBasket(httpContext, "1");
            controller.AddToBasket("1");

            Basket basket = baskets.Collection().FirstOrDefault();


            //Assert
            Assert.IsNotNull(basket);
            Assert.AreEqual(1, basket.BasketItems.Count);
            Assert.AreEqual("1", basket.BasketItems.ToList().FirstOrDefault().ProductId);
        }
Example #26
0
        public void Setup()
        {
            _addFromApprenticeshipDetailsModel = GetApprenticeshipDetailsRequestModel();
            _addFromApprenticeshipResultsModel = GetApprenticeshipResultsRequestModel();
            _addFromProviderDetailsModel       = GetProviderDetailsRequestModel();
            _addFromProviderSearchModel        = GetProviderResultsRequestModel();
            _deleteFromBasketViewModel         = GetDeleteFromBasketViewModel();

            // Set cookie in http request
            _mockMediator           = new Mock <IMediator>();
            _mockCookieManager      = new Mock <ICookieManager>();
            _mockBasketOrchestrator = new Mock <IBasketOrchestrator>();

            _mockBasketOrchestrator
            .Setup(orchestrator =>
                   orchestrator.UpdateBasket(It.IsAny <string>(), null, null))
            .ReturnsAsync(new AddOrRemoveFavouriteInBasketResponse());

            _mockMediator.Setup(s => s.Send(It.IsAny <GetBasketQuery>(), default(CancellationToken))).ReturnsAsync(GetApprenticeshipFavouritesBasketRead());

            _basketController = new BasketController(_mockMediator.Object, _mockCookieManager.Object, _mockBasketOrchestrator.Object);

            var httpContext = new DefaultHttpContext();
            var tempData    = new TempDataDictionary(httpContext, Mock.Of <ITempDataProvider>());

            _basketController.TempData = tempData;
        }
Example #27
0
        public void CanAddBasketItem()
        {
            //Arrange
            IRepository <Basket>   basketRepository   = new MockRepository <Basket>();
            IRepository <Product>  productRepository  = new MockRepository <Product>();
            IRepository <Order>    orderRepository    = new MockRepository <Order>();
            IRepository <Customer> customerRepository = new MockRepository <Customer>();
            MockHttpContext        httpContext        = new MockHttpContext();

            IBasketService basketService = new BasketService(productRepository, basketRepository);
            IOrderService  orderService  = new OrderService(orderRepository);

            var controller = new BasketController(basketService, orderService, customerRepository);

            controller.ControllerContext = new ControllerContext(httpContext, new RouteData(), controller);

            //Act
            // basketService.AddToBasket(httpContext, "1");
            controller.AddToBasket("1");

            Basket basket = basketRepository.Collection().FirstOrDefault();

            //Assert
            Assert.IsNotNull(basket);
            Assert.AreEqual(1, basket.BasketItems.Count);
            Assert.AreEqual("1", basket.BasketItems.ToList().FirstOrDefault().ProductId);
        }
Example #28
0
        public void CanAddBasketItem()
        {
            // Setup
            IRepository <Basket>   baskets   = new MockContext <Basket>();
            IRepository <Product>  products  = new MockContext <Product>();
            IRepository <Order>    orders    = new MockContext <Order>();
            IRepository <Customer> customers = new MockContext <Customer>();

            var httpContext = new MockHttpContext();

            IBasketService basketService = new BasketService(products, baskets);
            IOrderService  orderService  = new OrderService(orders);

            // This is so we can test add to bask throught the controller
            var controller = new BasketController(basketService, orderService, customers);

            controller.ControllerContext = new ControllerContext(httpContext, new System.Web.Routing.RouteData(), controller);

            // Act
            // This is to test the basket service itself
            //basketService.AddToBasket(httpContext, "1");
            controller.AddToBasket("1");

            Basket basket = baskets.Collection().FirstOrDefault();

            // Test
            // Test if Basket is not Empty
            Assert.IsNotNull(basket);

            // Test that there is only one item in the basket
            Assert.AreEqual(1, basket.BasketItems.Count);

            // Tests that the first item productid is 1
            Assert.AreEqual("1", basket.BasketItems.ToList().FirstOrDefault().ProductId);
        }
Example #29
0
        public void CanAddBasketItem()
        {
            //Arrange
            IRepository <Basket>   basketContext   = new MockContext <Basket>();
            IRepository <Product>  productContext  = new MockContext <Product>();
            IRepository <Order>    orderContext    = new MockContext <Order>();
            IRepository <Customer> customerContext = new MockContext <Customer>();
            HttpContextBase        httpContext     = new MockHttpContext();

            IBasketService basketService = new BasketService(basketContext, productContext);
            IOrderService  orderService  = new OrderService(orderContext);

            BasketController controller = new BasketController(basketService, orderService, customerContext);

            controller.ControllerContext = new System.Web.Mvc.ControllerContext(httpContext, new System.Web.Routing.RouteData(), controller);

            //Act
            basketService.AddToBasket(httpContext, "1");
            Basket basket = basketContext.Collection().FirstOrDefault();

            //Assert
            Assert.IsNotNull(basket);
            Assert.AreEqual(1, basket.BasketItems.Count());
            Assert.AreEqual("1", basket.BasketItems.ToList().FirstOrDefault().ProductId);
        }
        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();
            }
        }