public void Add_WhenCalled_ReturnsObjWithId()
        {
            //Arrange
            var input = new Api.Models.Order()
            {
                Price = 11.11M, CreatedDate = DateTime.Now
            };

            // Act
            var result   = _controller.Add(input);
            var okResult = (OkObjectResult)result;

            // Assert
            Assert.IsType <OkObjectResult>(result);
            Assert.IsType <Api.Models.Order>(okResult.Value);
            Assert.True(((Api.Models.Order)okResult.Value).Id > 0);
        }
        public async Task Add_WithAlreadyAddedLastAvailableSupplement_ShouldShowErrorMessageAndReturnToReturnUrl()
        {
            string       errorMessage = null;
            const string returnUrl    = "returnUrl";

            //Arrange
            Mock <IUrlHelper> urlHelper = new Mock <IUrlHelper>();

            urlHelper
            .Setup(u => u.IsLocalUrl(It.IsAny <string>()))
            .Returns(true);

            Mock <IOrderService> orderService = new Mock <IOrderService>();

            orderService
            .Setup(o => o.IsLastAvailableSupplementAlreadyAdded(supplementId, It.IsAny <ShoppingCart>()))
            .ReturnsAsync(true);

            Mock <ISupplementService> supplementService = new Mock <ISupplementService>();

            supplementService
            .Setup(s => s.IsSupplementExistingById(supplementId, false))
            .ReturnsAsync(true);

            Mock <ITempDataDictionary> tempData = new Mock <ITempDataDictionary>();

            tempData
            .SetupSet(t => t[TempDataErrorMessageKey] = It.IsAny <string>())
            .Callback((string key, object message) => errorMessage = message as string);

            Mock <ISession> session = new Mock <ISession>();

            Mock <HttpContext> httpContext = new Mock <HttpContext>();

            httpContext
            .SetupGet(h => h.Session)
            .Returns(session.Object);

            OrdersController ordersController = new OrdersController(null, orderService.Object, supplementService.Object)
            {
                Url               = urlHelper.Object,
                TempData          = tempData.Object,
                ControllerContext = new ControllerContext
                {
                    HttpContext = httpContext.Object
                }
            };

            //Act
            var result = await ordersController.Add(supplementId, returnUrl);

            //Assert
            errorMessage.Should().Be(SupplementLastOneJustAddedErrorMessage);

            result.Should().BeOfType <RedirectResult>();

            result.As <RedirectResult>().Url.Should().Be(returnUrl);
        }
        public async void ShouldCreateOrder()
        {
            var order    = CreateOrder();
            var orderDto = MapModelToDto(order);

            _orderServiceMock.Setup(o => o.Add(order)).Returns(Task.FromResult(true));
            _mapperMock.Setup(m => m.Map <Order>(It.IsAny <OrderDto>())).Returns(order);

            var result = await _orderController.Add(orderDto);

            Assert.IsType <ActionResult <OrderDto> >(result);
            Assert.IsType <OkObjectResult>(result.Result);
        }
        public void Add_ValidObjectPassed_ReturnedGetOneMoreItem()
        {
            // Arrange
            var testItem = new Order()
            {
                OrderId      = 99999,
                CustomerInfo = "Nickki",
                ItemInfo     = "Devil May Cry 5"
            };

            // Act
            var createdResponse = _controller.Add(testItem);
            var item            = createdResponse;

            // Assert
            Assert.Equal(4, _service.GetAll().Count());
        }
Beispiel #5
0
        public async Task TestPostOrderAsync()
        {
            var mockRepo = new Mock <IRepositoryWrapper>();

            var order = _fakeData.GetNewFakeOrder();

            mockRepo.Setup(repo => repo.Orders.GetById(1))
            .ReturnsAsync(_fakeData.GetFakeOrders().FirstOrDefault());

            var controller = new OrdersController(mockRepo.Object);



            var response = await controller.Add(order) as ObjectResult;

            // Check if created response
            Assert.Equal(201, response.StatusCode);
        }
        public async Task Add_WithIncorrectUrlAndIncorrectSupplementId_ShouldChangeReturnUrlAndShowErrorMessageAndReturnToHomeIndex()
        {
            string errorMessage       = null;
            string incorrectReturnUrl = null;

            //Arrange
            Mock <IUrlHelper> urlHelper = new Mock <IUrlHelper>();

            urlHelper
            .Setup(u => u.IsLocalUrl(It.IsAny <string>()))
            .Returns(false)
            .Callback((string url) => { incorrectReturnUrl = url; });

            Mock <ISupplementService> supplementService = new Mock <ISupplementService>();

            supplementService
            .Setup(s => s.IsSupplementExistingById(supplementId, false))
            .ReturnsAsync(false);

            Mock <ITempDataDictionary> tempData = new Mock <ITempDataDictionary>();

            tempData
            .SetupSet(t => t[TempDataErrorMessageKey] = It.IsAny <string>())
            .Callback((string key, object message) => errorMessage = message as string);

            OrdersController ordersController = new OrdersController(null, null, supplementService.Object)
            {
                Url      = urlHelper.Object,
                TempData = tempData.Object
            };

            //Act
            var result = await ordersController.Add(nonExistingSupplementId, returnUrl);

            //Assert
            errorMessage.Should().Be(string.Format(EntityNotFound, SupplementEntity));
            incorrectReturnUrl.Should().Be(returnUrl);

            result.Should().BeOfType <RedirectToActionResult>();

            result.As <RedirectToActionResult>().ActionName.Should().Be("Index");
            result.As <RedirectToActionResult>().ControllerName.Should().Be("Home");
        }
        public void Add_CallWith_ValidOrder_ShouldReturn_OkResult()
        {
            var result = _controller.Add(_validOrderInputResource);

            Assert.True(result is OkObjectResult);
        }