コード例 #1
0
        public void CartController_AddAlbumToCart()
        {
            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.AddAlbum(It.Is <int>(u => u > 0), It.Is <int>(t => t > 0)));
            moqCartService.Setup(m => m.AddAlbum(It.Is <int>(u => u == 0), It.Is <int>(t => t > 0))).Throws <CartServiceException>();
            moqCartService.Setup(m => m.AddAlbum(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.AddAlbum(3);

            Assert.IsFalse(result.Permanent);
            Assert.AreEqual("Cart", result.RouteValues["controller"]);
            Assert.AreEqual("Index", result.RouteValues["action"]);

            var result1 = cartController.AddAlbum(0);

            Assert.IsInstanceOfType(result1, typeof(HttpNotFoundResult));
        }