コード例 #1
0
        public void IndexTest_CanViewCartContents()
        {
            var cart = new Cart();
            var target = new CartController(null, null);

            var result = (CartIndexViewModel)target.Index(cart, "myUrl").Model;
            Assert.AreSame(result.Cart, cart);
            Assert.AreEqual(result.ReturnUrl, "myUrl");
        }
コード例 #2
0
        public void adding_product_redirects_to_index()
        {
            // arrange
            IProductsRepository repository = Substitute.For<IProductsRepository>();
            repository.Products.Returns(new List<Product> { new Product { ProductID = 123 } }.AsQueryable());
            CartController controller = new CartController(repository, null);

            // act
            var result = controller.AddProduct(123, null, new Cart());

            // assert
            result.ShouldBeRedirectionTo(new { action = "Index" });
        }
コード例 #3
0
 public void CheckoutTest_CannotCheckoutEmptyCart()
 {
     var mock = new Mock<IOrderProcessor>();
     var cart = new Cart();
     var target = new CartController(null, mock.Object);
     //动作
     ViewResult result = target.Checkout(cart, new ShippingDetails());
     //断言--检查,订单尚未传递给处理器
     mock.Verify(m => m.ProcessOrder(It.IsAny<Cart>(), It.IsAny<ShippingDetails>()), Times.Never);
     //断言--检查,该方法返回的默认视图
     Assert.AreEqual("", result.ViewName);
     //断言--检查,对视图传递一个非法模型
     Assert.AreEqual(false, result.ViewData.ModelState.IsValid);
 }
コード例 #4
0
        public void AddToCartTest_CanAddToCart()
        {
            var mock = new Mock<IProductRepository>();
            mock.Setup(m => m.Products).Returns(new Product[] {
                new Product{ ProductID=1,Name="P1", Category="Apples"}
            }.AsQueryable());

            var cart = new Cart();
            var target = new CartController(mock.Object, null);
            target.AddToCart(cart, 1, null);

            Assert.AreEqual(cart.Lines.Count(), 1);
            Assert.AreEqual(cart.Lines.ToArray()[0].Product.ProductID, 1);
        }
コード例 #5
0
        public void AddToCartTest_AddingProductToCartGoesToCartScreen()
        {
            var mock = new Mock<IProductRepository>();
            mock.Setup(m => m.Products).Returns(new Product[] {
                new Product{ ProductID=1,Name="P1", Category="Apples"}
            }.AsQueryable());

            var cart = new Cart();
            var target = new CartController(mock.Object, null);

            RedirectToRouteResult result = target.AddToCart(cart, 2, "myUrl");

            Assert.AreEqual(result.RouteValues["action"], "Index");
            Assert.AreEqual(result.RouteValues["returnUrl"], "myUrl");
        }
コード例 #6
0
ファイル: CartTests.cs プロジェクト: kreatora3/SportStoreRepo
        public void Cannot_Checkout_Empty_Cart()
        {
            Mock<IOrderProcessor> mock = new Mock<IOrderProcessor>();

            Cart cart = new Cart();
            ShippingDetails shippingDetails = new ShippingDetails();
            CartController target = new CartController(null, mock.Object);

            ViewResult result = target.Checkout(cart, shippingDetails);

            //Assert
            mock.Verify(m => m.ProcessOrder(It.IsAny<Cart>(), It.IsAny<ShippingDetails>()), Times.Never());
            Assert.AreEqual("", result.ViewName);
            Assert.AreEqual(false, result.ViewData.ModelState.IsValid);
        }
コード例 #7
0
        public void can_add_products_to_cart()
        {
            // arrange
            IProductsRepository repository = Substitute.For<IProductsRepository>();
            Product someProduct = new Product { ProductID= 123 };
            repository.Products.Returns( new List<Product> { someProduct }.AsQueryable() );
            Cart cart = new Cart();

            CartController controller = new CartController(repository, null);

            // act
            controller.AddProduct(123, null, cart);

            // assert
            cart.Lines[0].Product.ShouldEqual(someProduct);
        }
コード例 #8
0
ファイル: UnitTest1.cs プロジェクト: vanhp/SportStore
 public void Adding_Product_To_Cart_Goes_To_Cart_Screen()
 {
     // Arrange - create the mock repository
     Mock<IProductsRepository> mock = new Mock<IProductsRepository>();
     mock.Setup(m => m.Products).Returns(new Product[] {
         new Product {ProductID = 1, Name = "P1", Category = "Apples"},
         }.AsQueryable());
     // Arrange - create a Cart
     Cart cart = new Cart();
     // Arrange - create the controller
     CartController target = new CartController(mock.Object, null);
     // Act - add a product to the cart
     RedirectToRouteResult result = target.AddToCart(cart, 2, "myUrl");
     // Assert
     Assert.AreEqual(result.RouteValues["action"], "Index");
     Assert.AreEqual(result.RouteValues["returnUrl"], "myUrl");
 }
コード例 #9
0
ファイル: CartTests.cs プロジェクト: kreatora3/SportStoreRepo
        public void Cannot_Checkout_Invalid_ShippingDetails()
        {
            //Arrange
            Mock<IOrderProcessor> mock = new Mock<IOrderProcessor>();
            Cart cart = new Cart();
            cart.AddItem(new Product(), 1);

            CartController target = new CartController(null, mock.Object);
            target.ModelState.AddModelError("error", "error");

            //Act
            ViewResult result = target.Checkout(cart, new ShippingDetails());

            mock.Verify(m => m.ProcessOrder(It.IsAny<Cart>(), It.IsAny<ShippingDetails>()), Times.Never);
            Assert.AreEqual("", result.ViewName);
            Assert.AreEqual(false, result.ViewData.ModelState.IsValid);
        }
コード例 #10
0
        public void CheckoutTest_CannotCheckoutInvalidShippingDetails()
        {
            var mock = new Mock<IOrderProcessor>();
            var cart = new Cart();
            cart.AddItem(new Product(), 1);

            var target = new CartController(null, mock.Object);
            //准备--把一个错误添加到模型
            target.ModelState.AddModelError("error", "error");
            //动作--试图结算
            ViewResult result = target.Checkout(cart, new ShippingDetails());

            //断言--检查,订单尚未传递给处理器
            mock.Verify(m => m.ProcessOrder(It.IsAny<Cart>(), It.IsAny<ShippingDetails>()), Times.Never);
            //断言--检查,该方法返回的默认视图
            Assert.AreEqual("", result.ViewName);
            //断言--检查,对视图传递一个非法模型
            Assert.AreEqual(false, result.ViewData.ModelState.IsValid);
        }
コード例 #11
0
ファイル: UnitTest1.cs プロジェクト: vanhp/SportStore
 public void Cannot_Checkout_Invalid_ShippingDetails()
 {
     // Arrange - create a mock order processor
     Mock<IOrderProcessor> mock = new Mock<IOrderProcessor>();
     // Arrange - create a cart with an item
     Cart cart = new Cart();
     cart.AddItem(new Product(), 1);
     // Arrange - create an instance of the controller
     CartController target = new CartController(null, mock.Object);
     // Arrange - add an error to the model
     target.ModelState.AddModelError("error", "error");
     // Act - try to checkout
     ViewResult result = target.Checkout(cart, new ShippingDetails());
     // Assert - check that the order hasn't been passed on to the processor
     mock.Verify(m => m.ProcessOrder(It.IsAny<Cart>(), It.IsAny<ShippingDetails>()), Times.Never());
     // Assert - check that the method is returning the default view
     Assert.AreEqual("", result.ViewName);
     // Assert - check that I am passing an invalid model to the view
     Assert.AreEqual(false, result.ViewData.ModelState.IsValid);
 }
コード例 #12
0
ファイル: UnitTest1.cs プロジェクト: vanhp/SportStore
        public void Cannot_Checkout_Empty_Cart()
        {
            // Arrange - create a mock order processor
            Mock<IOrderProcessor> mock = new Mock<IOrderProcessor>();
            // Arrange - create an empty cart
            Cart cart = new Cart();
            // Arrange - create shipping details
            ShippingDetails shippingDetails = new ShippingDetails();
            // Arrange - create an instance of the controller
            CartController target = new CartController(null, mock.Object);

            // Act
            ViewResult result = target.Checkout(cart, shippingDetails);

            // Assert - check that the order hasn't been passed on to the processor
            mock.Verify(m => m.ProcessOrder(It.IsAny<Cart>(), It.IsAny<ShippingDetails>()), Times.Never());
            // Assert - check that the method is returning the default view
            Assert.AreEqual("", result.ViewName);
            // Assert - check that I am passing an invalid model to the view
            Assert.AreEqual(false, result.ViewData.ModelState.IsValid);
        }
コード例 #13
0
ファイル: CartTests.cs プロジェクト: YuraChubenets/SportStore
        public void Adding_Game_To_Cart_Goes_To_Cart_Screen()
        {
            //arrange
            Mock<IGameRepository> mock =
                new Mock<IGameRepository>();
            mock.Setup(m => m.Games).Returns(new List<Game>
                       {
                            new Game { GameId=1, Name="Game1", Category="Categ1" },
                            new Game {GameId=2,  Name="Game2", Category="Categ2"  },
                            new Game {GameId=3,  Name="Game3", Category="Categ1" },
                            new Game {GameId=4,  Name="Game4", Category="Categ4" },
                            new Game {GameId=5,  Name="Game5", Category="Categ2" }
                        });

            Cart cart = new Cart();

            CartController ctrl = new CartController(mock.Object, null);
            //Act
            RedirectToRouteResult result = ctrl.AddToCart(cart, 2, "myUrl");

            // Утверждение
            Assert.AreEqual(result.RouteValues["action"], "Index");
            Assert.AreEqual(result.RouteValues["returnUrl"], "myUrl");
        }
コード例 #14
0
ファイル: UnitTest1.cs プロジェクト: vanhp/SportStore
 public void Can_View_Cart_Contents()
 {
     // Arrange - create a Cart
     Cart cart = new Cart();
     // Arrange - create the controller
     CartController target = new CartController(null, null);
     // Act - call the Index action method
     CartIndexViewModel result = (CartIndexViewModel)target.Index(cart, "myUrl").ViewData.Model;
     // Assert
     Assert.AreSame(result.Cart, cart);
     Assert.AreEqual(result.ReturnUrl, "myUrl");
 }
コード例 #15
0
ファイル: CartTest.cs プロジェクト: recovery098/SportStore
        public void Can_View_Cart_Content()
        {
            Cart cart = new Cart();

            CartController target = new CartController(null,null);

            CartIndexViewModel result = (CartIndexViewModel)target.Index(cart, "myUrl").ViewData.Model;

            Assert.AreSame(result.Cart, cart);
            Assert.AreEqual(result.ReturnUrl, "myUrl");
        }
コード例 #16
0
ファイル: CartTests.cs プロジェクト: YuraChubenets/SportStore
        public void Can_Add_To_Cart()
        {
            //arrange
            Mock < IGameRepository > mock =
                new Mock<IGameRepository>();
            mock.Setup(m => m.Games).Returns(new List<Game>
                       {
                            new Game { GameId=1, Name="Game1", Category="Categ1" },
                            new Game {GameId=2,  Name="Game2", Category="Categ2"  },
                            new Game {GameId=3,  Name="Game3", Category="Categ1" },
                            new Game {GameId=4,  Name="Game4", Category="Categ4" },
                            new Game {GameId=5,  Name="Game5", Category="Categ2" }
                        });

            Cart cart = new Cart();

             CartController ctrl = new CartController(mock.Object, null);
            //Act

            ctrl.AddToCart(cart, 1, null);

            //assert
            Assert.AreEqual(cart.Lines.Count(), 1);
            Assert.AreEqual(cart.Lines.ToList()[0].Game.GameId, 1);
        }
コード例 #17
0
ファイル: CartTests.cs プロジェクト: YuraChubenets/SportStore
        public void Can_View_Cart_Contents()
        {
            //arrange

            Cart cart = new Cart();

            CartController ctrl = new CartController(null,null);
            //Act
            CartIndexViewModel result
                  = (CartIndexViewModel)ctrl.Index(cart, "myUrl").ViewData.Model;

            //assert
            Assert.AreSame(result.Cart, cart);
            Assert.AreEqual(result.ReturnUrl, "myUrl");
        }
コード例 #18
0
        public void CheckoutTest_CannotCheckoutAndSubmitOrder()
        {
            var mock = new Mock<IOrderProcessor>();
            var cart = new Cart();
            cart.AddItem(new Product(), 1);

            var target = new CartController(null, mock.Object);
            //动作--试图结算
            var result = target.Checkout(cart, new ShippingDetails());

            //断言--检查,订单已经传递给处理器
            mock.Verify(m => m.ProcessOrder(It.IsAny<Cart>(), It.IsAny<ShippingDetails>()), Times.Once);
            //断言--检查,该方法返回Completed视图
            Assert.AreEqual("Completed", result.ViewName);
            //断言--检查,对视图传递一个有效的模型
            Assert.AreEqual(true, result.ViewData.ModelState.IsValid);
        }
コード例 #19
0
ファイル: CartTest.cs プロジェクト: recovery098/SportStore
        public void Adding_Product_Goes_To_Cart_Screen()
        {
            Mock<IProductRepository> mock = new Mock<IProductRepository>();
            mock.Setup(m => m.Products).Returns(new Product[]
            {
                new Product {ProductID=1, Name="P1", Category = "Jab" },
            }.AsQueryable());

            Cart cart = new Cart();

            CartController target = new CartController(mock.Object,null);

            RedirectToRouteResult result = target.AddToCart(cart, 1, "myUrl");

            Assert.AreEqual(result.RouteValues["action"], "Index");
            Assert.AreEqual(result.RouteValues["returnUrl"], "myUrl");

        }
コード例 #20
0
ファイル: UnitTest1.cs プロジェクト: vanhp/SportStore
        public void Can_Add_To_Cart()
        {
            // Arrange - create the mock repository
            Mock<IProductsRepository> mock = new Mock<IProductsRepository>();
            mock.Setup(m => m.Products).Returns(
                new Product[]
                {
                    new Product { ProductID = 1, Name = "P1", Category = "Apples"}
                }.AsQueryable());
            // Arrange - create a Cart
            Cart cart = new Cart();
            // Arrange - create the controller
            CartController target = new CartController(mock.Object, null);

            // Act - add a product to the cart
            target.AddToCart(cart, 1, null);
            // Assert
            Assert.AreEqual(cart.Lines.Count(), 1);
            Assert.AreEqual(cart.Lines.ToArray()[0].Product.ProductID, 1);
        }
コード例 #21
0
ファイル: CartTest.cs プロジェクト: recovery098/SportStore
        public void Can_Checkout_And_Submit_Order()
        {
            Mock<IOrderProcessor> mock = new Mock<IOrderProcessor>();

            Cart cart = new Cart();
            cart.AddItem(new Product(), 1); 

            CartController target = new CartController(null, mock.Object);

            ViewResult result = target.Checkout(cart, new ShippingDetails());

            mock.Verify(m => m.ProcessOrder(It.IsAny<Cart>(), It.IsAny<ShippingDetails>()), Times.Once);

            Assert.AreEqual("Completed", result.ViewName);
            Assert.AreEqual(true, result.ViewData.ModelState.IsValid);

        }
コード例 #22
0
ファイル: UnitTest1.cs プロジェクト: vanhp/SportStore
 public void Can_Checkout_And_Submit_Order()
 {
     // Arrange - create a mock order processor
     Mock<IOrderProcessor> mock = new Mock<IOrderProcessor>();
     // Arrange - create a cart with an item
     Cart cart = new Cart();
     cart.AddItem(new Product(), 1);
     // Arrange - create an instance of the controller
     CartController target = new CartController(null, mock.Object);
     // Act - try to checkout
     ViewResult result = target.Checkout(cart, new ShippingDetails());
     // Assert - check that the order has been passed on to the processor
     mock.Verify(m => m.ProcessOrder(It.IsAny<Cart>(),
     It.IsAny<ShippingDetails>()), Times.Once());
     // Assert - check that the method is returning the Completed view
     Assert.AreEqual("Completed", result.ViewName);
     // Assert - check that I am passing a valid model to the view
     Assert.AreEqual(true, result.ViewData.ModelState.IsValid);
 }