public void AddDeliveryPost_CorrectDelivery_ReturnsCorrectRedirectToRouteResult() { // Arrange Mock <IDeliveryProvider> deliveryProviderMock = new Mock <IDeliveryProvider>(); Mock <IDeliveryHandler> deliveryHandlerMock = new Mock <IDeliveryHandler>(); deliveryHandlerMock.Setup(x => x.Add(It.IsAny <Delivery>())) .Returns(1); Mock <IDeliveryValidator> deliveryValidatorMock = new Mock <IDeliveryValidator>(); deliveryValidatorMock.Setup(x => x.CanAddDelivery(It.IsAny <Delivery>())) .Returns(new List <KeyValuePair <string, string> >()); DeliveryController controller = new DeliveryController(deliveryProviderMock.Object, deliveryHandlerMock.Object, deliveryValidatorMock.Object); // Act RedirectToRouteResult result = controller.AddDeliveryPost(this.deliveries[0]) as RedirectToRouteResult; // Assert Assert.IsNotNull(result); Assert.AreEqual("GetDeliveriesList", result.RouteValues.FirstOrDefault().Value); }
public void AddDeliveryPost_NullDelivery_ThrowsException() { // Arrange Mock <IDeliveryProvider> deliveryProviderMock = new Mock <IDeliveryProvider>(); Mock <IDeliveryHandler> deliveryHandlerMock = new Mock <IDeliveryHandler>(); Mock <IDeliveryValidator> deliveryValidatorMock = new Mock <IDeliveryValidator>(); deliveryValidatorMock.Setup(x => x.CanAddDelivery(null)) .Throws <ArgumentNullException>(); DeliveryController controller = new DeliveryController(deliveryProviderMock.Object, deliveryHandlerMock.Object, deliveryValidatorMock.Object); // Act ViewResult result = controller.AddDeliveryPost(null) as ViewResult; // Assert Assert.IsNotNull(result); Assert.IsFalse(controller.ModelState.IsValid); Assert.AreEqual(1, controller.ModelState.Keys.Count); Assert.AreEqual("", controller.ModelState.Keys.FirstOrDefault()); Assert.AreEqual("AddDelivery", result.ViewName); }