public void Should_Create_Order_Time_Error() { var input = "afternoon, 1, 2, 3"; var inputArray = new string[4] { "afternoon", "1", "2", "3" }; var output = new DishOutput() { OrderError = "The selected time of day is not valid." }; // Arrange var mockService = new Mock <IOrderRepository>(); mockService.Setup(repo => repo.GetTypeOfDish(inputArray[0])).Returns((Dish[])null); var orderService = new OrderService(mockService.Object); // Act var result = orderService.CreateOrder(input); // Assert Assert.NotNull(result); Assert.Equal("The selected time of day is not valid.", result.OrderError); }
public void Should_Create_Order() { var input = "morning, 1, 2, 3"; var inputArray = new string[4] { "morning", "1", "2", "3" }; var output = new DishOutput() { OrderResponse = "eggs, toast, coffee" }; Dish[] MorningDishes = new Dish[] { new Dish(DishEnum.Entree, "eggs"), new Dish(DishEnum.Side, "toast"), new Dish(DishEnum.Drink, "coffee", true) }; // Arrange var mockService = new Mock <IOrderRepository>(); mockService.Setup(repo => repo.GetTypeOfDish(inputArray[0])).Returns(MorningDishes); mockService.Setup(repo => repo.GetDishes(MorningDishes, inputArray)).Returns("eggs, toast, coffee"); var orderService = new OrderService(mockService.Object); // Act var result = orderService.CreateOrder(input); // Assert Assert.NotNull(result); Assert.Equal("eggs, toast, coffee", result.OrderResponse); }
public void Should_Get_Order_Error() { var input = "morning"; var output = new DishOutput() { OrderError = "The order must have at least one item." }; // Arrange var mockService = new Mock <IOrderService>(); mockService.Setup(repo => repo.CreateOrder(input)).Returns(output); var controller = new OrderController(mockService.Object); // Act var result = controller.Get(input) as BadRequestObjectResult; // Assert Assert.NotNull(result); Assert.Equal(400, result.StatusCode); Assert.Equal("The order must have at least one item.", (result.Value as DishOutput).OrderError); }
public void Should_Get_Order_With_Space() { var input = "morning,1,2 ,3"; var output = new DishOutput() { OrderResponse = "eggs, toast, coffee" }; // Arrange var mockService = new Mock <IOrderService>(); mockService.Setup(repo => repo.CreateOrder(input)).Returns(output); var controller = new OrderController(mockService.Object); // Act var result = controller.Get(input) as OkObjectResult; // Assert Assert.NotNull(result); Assert.Equal(200, result.StatusCode); Assert.Equal("eggs, toast, coffee", (result.Value as DishOutput).OrderResponse); }
public void Should_Get_Order_Invalid_Time_Error() { var input = "afternoon, 1, 2, 3"; var output = new DishOutput() { OrderError = "The selected time of day is not valid." }; // Arrange var mockService = new Mock <IOrderService>(); mockService.Setup(repo => repo.CreateOrder(input)).Returns(output); var controller = new OrderController(mockService.Object); // Act var result = controller.Get(input) as BadRequestObjectResult; // Assert Assert.NotNull(result); Assert.Equal(400, result.StatusCode); Assert.Equal("The selected time of day is not valid.", (result.Value as DishOutput).OrderError); }
public DishOutput CreateOrder(string input) { var order = NormalizeOrder(input); var menuType = _orderRepository.GetTypeOfDish(order[0]); var response = new DishOutput(); if (menuType == null) { response.OrderError = "The selected time of day is not valid."; return(response); } try { response.OrderResponse = _orderRepository.GetDishes(menuType, order); return(response); } catch (TargetInvocationException ex) { if (ex.InnerException.GetType() == typeof(FormatException)) { response.OrderError = "One of the selected dishes is not valid."; } else if (ex.InnerException.GetType() == typeof(InvalidOperationException)) { response.OrderError = "The order must have at least one item."; } else { ExceptionDispatchInfo.Capture(ex.InnerException).Throw(); } return(response); } }