コード例 #1
0
        public void CanDeleteValidProducts()
        {
            // Arrange - create a Product
            var prod = new Product { ProductID = 2, Name = "Test" };

            // Arrange - create the mock repository
            var mock = new Mock<IProductRepository>();
            mock.Setup(m => m.Products).Returns(
                new[] { new Product { ProductID = 1, Name = "P1" }, prod, new Product { ProductID = 3, Name = "P3" }, }.
                    AsQueryable());

            // Arrange - create the controller
            var target = new AdminController(mock.Object);

            // Act - delete the product
            target.Delete(prod.ProductID);

            // Assert - ensure that the repository delete method was
            // called with the correct Product
            mock.Verify(m => m.DeleteProduct(prod));
        }
コード例 #2
0
        public void CanEditProduct()
        {
            // Arrange - create the mock repository
            var mock = new Mock<IProductRepository>();
            mock.Setup(m => m.Products).Returns(
                new[]
                    {
                        new Product { ProductID = 1, Name = "P1" }, new Product { ProductID = 2, Name = "P2" },
                        new Product { ProductID = 3, Name = "P3" },
                    }.AsQueryable());

            // Arrange - create the controller
            var target = new AdminController(mock.Object);

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

            // Assert
            Assert.AreEqual(1, p1.ProductID);
            Assert.AreEqual(2, p2.ProductID);
            Assert.AreEqual(3, p3.ProductID);
        }
コード例 #3
0
        public void CannotDeleteInvalidProducts()
        {
            // Arrange - create the mock repository
            var mock = new Mock<IProductRepository>();
            mock.Setup(m => m.Products).Returns(
                new[]
                    {
                        new Product { ProductID = 1, Name = "P1" }, new Product { ProductID = 2, Name = "P2" },
                        new Product { ProductID = 3, Name = "P3" },
                    }.AsQueryable());

            // Arrange - create the controller
            var target = new AdminController(mock.Object);

            // Act - delete using an ID that doesn't exist
            target.Delete(100);

            // Assert - ensure that the repository delete method was
            // called with the correct Product
            mock.Verify(m => m.DeleteProduct(It.IsAny<Product>()), Times.Never());
        }
コード例 #4
0
        public void IndexContainsAllProducts()
        {
            // Arrange - create the mock repository
            var mock = new Mock<IProductRepository>();
            mock.Setup(m => m.Products).Returns(
                new[]
                    {
                        new Product { ProductID = 1, Name = "P1" }, new Product { ProductID = 2, Name = "P2" },
                        new Product { ProductID = 3, Name = "P3" },
                    }.AsQueryable());

            // Arrange - create a controller
            var target = new AdminController(mock.Object);

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

            // Assert
            Assert.AreEqual(result.Length, 3);
            Assert.AreEqual("P1", result[0].Name);
            Assert.AreEqual("P2", result[1].Name);
            Assert.AreEqual("P3", result[2].Name);
        }
コード例 #5
0
        public void CanSaveValidChanges()
        {
            // Arrange - create mock repository
            var mock = new Mock<IProductRepository>();

            // Arrange - create the controller
            var target = new AdminController(mock.Object);

            // Arrange - create a product
            var product = new Product { Name = "Test" };

            // Act - try to save the product
            ActionResult result = target.Edit(product, null);

            // Assert - check that the repository was called
            mock.Verify(m => m.SaveProduct(product));

            // Assert - check the method result type
            Assert.IsNotInstanceOfType(result, typeof(ViewResult));
        }
コード例 #6
0
        public void CannotSaveInvalidChanges()
        {
            // Arrange - create mock repository
            var mock = new Mock<IProductRepository>();

            // Arrange - create the controller
            var target = new AdminController(mock.Object);

            // Arrange - create a product
            var product = new Product { Name = "Test" };

            // Arrange - add an error to the model state
            target.ModelState.AddModelError("error", "error");

            // Act - try to save the product
            ActionResult result = target.Edit(product, null);

            // Assert - check that the repository was not called
            mock.Verify(m => m.SaveProduct(It.IsAny<Product>()), Times.Never());

            // Assert - check the method result type
            Assert.IsInstanceOfType(result, typeof(ViewResult));
        }
コード例 #7
0
        public void CannotEditNonexistentProduct()
        {
            // Arrange - create the mock repository
            var mock = new Mock<IProductRepository>();
            mock.Setup(m => m.Products).Returns(
                new[]
                    {
                        new Product { ProductID = 1, Name = "P1" }, new Product { ProductID = 2, Name = "P2" },
                        new Product { ProductID = 3, Name = "P3" },
                    }.AsQueryable());

            // Arrange - create the controller
            var target = new AdminController(mock.Object);

            // Act
            var result = (Product)target.Edit(4).ViewData.Model;

            // Assert
            Assert.IsNull(result);
        }