コード例 #1
0
        public void Can_Save_Valid_Changes()
        {
            // Arrange
            Mock<IProductsRepository> mock = new Mock<IProductsRepository>();
            AdminController target = new AdminController(mock.Object);
            Product product = new Product { Name = "Test" };

            // Act
            ActionResult result = target.Edit(product,null);

            // Assert
            mock.Verify(m => m.SaveProduct(product));
            Assert.IsNotInstanceOfType(result, typeof(ViewResult));
        }
コード例 #2
0
        public void Index_Contains_All_Products()
        {
            Mock<IProductsRepository> mock = new Mock<IProductsRepository>();
            mock.Setup(m => m.Products).Returns(new Product[]
                {
                    new Product { ProductID = 1, Name = "P1"},
                    new Product { ProductID = 2, Name = "P2"},
                    new Product { ProductID = 3, Name = "P3"}
                }.AsQueryable());
            AdminController target = new AdminController(mock.Object);

            Product[] result = ((IEnumerable<Product>)target.Index().ViewData.Model).ToArray();

            Assert.AreEqual(result.Length, 3);
            Assert.AreEqual("P1", result[0].Name);
            Assert.AreEqual("P2", result[1].Name);
            Assert.AreEqual("P3", result[2].Name);
        }
コード例 #3
0
        public void Can_Edit_Product()
        {
            Mock<IProductsRepository> mock = new Mock<IProductsRepository>();
            mock.Setup(m => m.Products).Returns(new Product[]
                {
                    new Product { ProductID = 1, Name = "P1"},
                    new Product { ProductID = 2, Name = "P2"},
                    new Product { ProductID = 3, Name = "P3"}
                }.AsQueryable());
            AdminController target = new AdminController(mock.Object);

            Product p1 = target.Edit(1).ViewData.Model as Product;
            Product p2 = target.Edit(2).ViewData.Model as Product;
            Product p3 = target.Edit(3).ViewData.Model as Product;

            Assert.AreEqual(1, p1.ProductID);
            Assert.AreEqual(2, p2.ProductID);
            Assert.AreEqual(3, p3.ProductID);
        }
コード例 #4
0
        public void Can_Delete_Valid_Products()
        {
            // Arrange
            Product prod = new Product { ProductID = 2, Name = "Test" };
            Mock<IProductsRepository> mock = new Mock<IProductsRepository>();
            mock.Setup(m => m.Products).Returns(new Product[]
                {
                    new Product { ProductID = 1, Name = "P1"},
                    prod,
                    new Product { ProductID = 3, Name = "P3"}
                }.AsQueryable());
            AdminController target = new AdminController(mock.Object);

            // Act
            target.Delete(prod.ProductID);

            // Assert
            mock.Verify(m => m.DeleteProduct(prod));
        }
コード例 #5
0
        public void Cannot_Save_Invalid_Changes()
        {
            // Arrange
            Mock<IProductsRepository> mock = new Mock<IProductsRepository>();
            AdminController target = new AdminController(mock.Object);
            Product product = new Product { Name = "Test" };
            target.ModelState.AddModelError("error", "error");

            // Act
            ActionResult result = target.Edit(product,null);

            // Assert
            mock.Verify(m => m.SaveProduct(It.IsAny<Product>()), Times.Never());
            Assert.IsInstanceOfType(result, typeof(ViewResult));
        }
コード例 #6
0
        public void Cannot_Edit_Nonexistent_Product()
        {
            Mock<IProductsRepository> mock = new Mock<IProductsRepository>();
            mock.Setup(m => m.Products).Returns(new Product[]
                {
                    new Product {ProductID = 1, Name = "P1"},
                    new Product {ProductID = 2, Name = "P2"},
                    new Product {ProductID = 3, Name = "P3"}
                }.AsQueryable());
            AdminController target = new AdminController(mock.Object);
            Product result = (Product)target.Edit(4).ViewData.Model;

            Assert.IsNull(result);
        }
コード例 #7
0
        public void Cannot_Delete_Invalid_Products()
        {
            // Arrange
            Mock<IProductsRepository> mock = new Mock<IProductsRepository>();
            mock.Setup(m => m.Products).Returns(new Product[]
                {
                    new Product { ProductID = 1, Name = "P1"},
                    new Product { ProductID = 2, Name = "P2"},
                    new Product { ProductID = 3, Name = "P3"}
                }.AsQueryable());
            AdminController target = new AdminController(mock.Object);

            // Act
            target.Delete(100);

            // Assert
            mock.Verify(m => m.DeleteProduct(It.IsAny<Product>()), Times.Never());
        }