public async Task AddOrder_ShouldReturnNewOrderId() { var result = await _orderController.AddOrder(_order) as CreatedAtActionResult; var order = result.Value as Order; Assert.NotEqual(0, order.Id); }
public void EnterNewOrderPageRenderSuccess() { var controller = new OrdersController(); var result = controller.AddOrder(); Assert.IsAssignableFrom(typeof(ViewResult), result); }
public async Task AddOrder_ValidParams_ReturnsCreated() { var order = new OrderDto() { Price = -234.45m, CreatedDate = DateTime.Today }; var orderWithId = new OrderDto() { Id = 1, Price = -234.45m, CreatedDate = DateTime.Today }; var mapper = Substitute.For <IMapper>(); var mockRepository = Substitute.For <ICustomersRepository>(); mockRepository.AddOrderAsync(1, order).Returns(orderWithId); var controller = new OrdersController(mockRepository); var response = await controller.AddOrder(1, order); response.Should().NotBeNull(); response.Result.Should().BeOfType <CreatedAtRouteResult>() .Which.StatusCode.Should().Be((int)HttpStatusCode.Created); var ok = response.Result as CreatedAtRouteResult; ok.RouteValues["Id"].Should().Be(orderWithId.Id); ok.Value.Should().BeOfType <OrderDto>(); ok.Value.Should().BeEquivalentTo(orderWithId, opt => opt.IgnoringCyclicReferences()); await mockRepository.Received().AddOrderAsync(1, order); }
public async Task GivenAValidOrder_WhenIPostANewInvalidOrder_ThenItReturnsBadRequest() { //Arrange _mockOrderMapper.Setup(mapper => mapper.Map <TblOrder>(It.IsAny <OrderForMappingDto>())) .Returns(new TblOrder()); _mockOrderRepository.Setup(repo => repo.AddOrder(It.IsAny <TblOrder>())) .ReturnsAsync((TblOrder order) => order); var SUT = new OrdersController(_mockOrderRepository.Object, _mockOrderMapper.Object); //Act var contentResult = await SUT.AddOrder(new OrderForMappingDto { ACustomerId = 3, AMovieId = 18, ARentalOrNot = false, //AOrderedDate = DateTime.Now }); //Assert //Assert.AreEqual(400, contentResult.StatusCode); Assert.IsInstanceOf <BadRequestObjectResult>(contentResult); }
public void EnterNewOrderPostToMockDb() { var item = new Item { item_id = Guid.NewGuid(), name = "Item1", price = 20.00M, quantity = 20 }; var cust = new Customer { customer_id = Guid.NewGuid(), name = "Test1", billing_address = " 123 First Street", billing_city = "Reynoldsburg", billing_state = "OH", billing_zip = "43221", isActive = true }; var or_id = Guid.NewGuid(); var new_order = new Order { order_id = or_id, item_id = item.item_id, quantity = 5, total_price = 100.00M, order_date = DateTime.Today, shipping_date = DateTime.Today.AddDays(8), city = "Dayton", street_address = "123 Fifth Avenue", state = "OH", customer_id = cust.customer_id }; var mockOrder = new Mock <DbSet <Order> >(); mockOrder.Setup(m => m.Find(or_id)).Returns(new_order); var moqItem = new Mock <DbSet <Item> >(); moqItem.Setup(m => m.Find(item.item_id)).Returns(item); var moqCust = new Mock <DbSet <Customer> >(); moqCust.Setup(m => m.Find(cust.customer_id)).Returns(cust); var mockContext = new Mock <StoreDbContext>(); mockContext.Setup(m => m.Orders).Returns(mockOrder.Object); mockContext.Setup(m => m.Items).Returns(moqItem.Object); mockContext.Setup(m => m.Customers).Returns(moqCust.Object); var vm = new OrderViewModel(new_order); var controller = new OrdersController(mockContext.Object); var result = controller.AddOrder(vm); mockContext.Verify(m => m.SaveChanges()); mockOrder.Verify(m => m.Add(It.IsAny <Order>()), Times.Once()); Assert.IsAssignableFrom(typeof(RedirectToRouteResult), result); }
public async Task GivenAValidOrder_WhenIPostANewOrder_ThenItReturnsOkWithResponse() { TblOrder expectedorder = new TblOrder { ACustomerId = 3, AMovieId = 18, ARentalOrNot = false, AOrderedDate = DateTime.Now }; //Arrange _mockOrderMapper.Setup(mapper => mapper.Map <TblOrder>(It.IsAny <OrderForMappingDto>())) .Returns(expectedorder); _mockOrderRepository.Setup(repo => repo.AddOrder(It.IsAny <TblOrder>())) .ReturnsAsync((TblOrder order) => order); var SUT = new OrdersController(_mockOrderRepository.Object, _mockOrderMapper.Object); //Act var contentResult = await SUT.AddOrder(new OrderForMappingDto { ACustomerId = 3, AMovieId = 18, ARentalOrNot = false, AOrderedDate = DateTime.Now }); var okResult = contentResult as OkObjectResult; //Assert Assert.AreEqual(200, okResult.StatusCode); Assert.NotNull(contentResult); Assert.IsAssignableFrom <OkObjectResult>(contentResult); var result = ((OkObjectResult)contentResult).Value; Assert.NotNull(result); Assert.IsAssignableFrom <TblOrder>(result); Assert.AreEqual(result, expectedorder); }
public async Task AddOrder_InvalidParams_ReturnsBadRequest() { var order = new OrderDto() { Price = -234.45m, CreatedDate = DateTime.Today }; var mapper = Substitute.For <IMapper>(); var mockRepository = Substitute.For <ICustomersRepository>(); mockRepository.AddOrderAsync(1, order).Throws <ValidationException>(); var controller = new OrdersController(mockRepository); Func <Task <ActionResult <OrderDto> > > response = async() => await controller.AddOrder(1, order); await response.Should().ThrowAsync <ValidationException>(); await mockRepository.Received().AddOrderAsync(1, order); }