public void CartController_DeleteTrackFromCart() { Mock <IOrderTrackRepository> moqOrderTrackRepository = new Mock <IOrderTrackRepository>(); Mock <IRepositoryFactory> moqRepositoryFactory = new Mock <IRepositoryFactory>(); moqRepositoryFactory.Setup(m => m.GetOrderTrackRepository()).Returns(moqOrderTrackRepository.Object); Mock <ICartService> moqCartService = new Mock <ICartService>(); moqCartService.Setup(m => m.RemoveTrack(It.Is <int>(u => u > 0), It.Is <int>(t => t > 0))); moqCartService.Setup(m => m.RemoveTrack(It.Is <int>(u => u == 0), It.Is <int>(t => t > 0))).Throws <CartServiceException>(); moqCartService.Setup(m => m.RemoveTrack(It.Is <int>(u => u > 0), It.Is <int>(t => t == 0))).Throws <CartServiceException>(); Mock <IServiceFactory> moqServiceFactory = new Mock <IServiceFactory>(); moqServiceFactory.Setup(m => m.GetCartService()).Returns(moqCartService.Object); var cartController = new CartController(moqRepositoryFactory.Object, moqServiceFactory.Object); var context = TestInitialize(); cartController.ControllerContext = new ControllerContext(context.Object, new RouteData(), cartController); var result = (RedirectToRouteResult)cartController.DeleteTrack(3); Assert.IsFalse(result.Permanent); Assert.AreEqual("Cart", result.RouteValues["controller"]); Assert.AreEqual("Index", result.RouteValues["action"]); var result1 = cartController.DeleteTrack(0); Assert.IsInstanceOfType(result1, typeof(HttpNotFoundResult)); }