public void EditTest_CanSaveValidChanges()
 {
     var mock = new Mock<IProductRepository>();
     var target = new AdminController(mock.Object);
     var product = new Product { Name = "Test" };
     ActionResult actual = target.Edit(product, null);
     ////检查是否调用了存储库
     mock.Verify(m => m.SaveProduct(product));
     ////检查方法的结果类型
     Assert.IsNotInstanceOfType(actual, typeof(ViewResult));
 }
 public void EditTest_ProductIdNotExist_CannotEdit()
 {
     var mock = new Mock<IProductRepository>();
     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());
     var target = new AdminController(mock.Object);
     Product result = (Product)target.Edit(4).ViewData.Model;
     Assert.IsNull(result);
 }
 public void EditTest_CannotSaveInvalidChanges()
 {
     var mock = new Mock<IProductRepository>();
     var target = new AdminController(mock.Object);
     var product = new Product { Name = "Test" };
     target.ModelState.AddModelError("error", "error");
     ActionResult actual = target.Edit(product, null);
     ////检查是否调用了存储库
     mock.Verify(m => m.SaveProduct(It.IsAny<Product>()), Times.Never());
     ////检查方法的结果类型
     Assert.IsInstanceOfType(actual, typeof(ViewResult));
 }
Esempio n. 4
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"}
            });

            AdminController target = new AdminController(mock.Object);

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

            Assert.IsNull(result);
        }
 public void EditTest_ProductIDExists_CanEdit()
 {
     var mock = new Mock<IProductRepository>();
     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);
 }
        public void IndexTest()
        {
            var mock = new Mock<IProductRepository>();
            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());
            var 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);
        }
Esempio n. 7
0
        public void Can_Delete_Valid_Products()
        {
            Product product = 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"},
                new Product{ProductID = 3, Name = "P3"}
            });

            AdminController target = new AdminController(mock.Object);

            target.Delete(product.ProductID);

            mock.Verify(m => m.DeleteProduct(product.ProductID));
        }
Esempio n. 8
0
        public void Cannot_Save_Invalid_Changes()
        {
            Mock<IProductsRepository> mock = new Mock<IProductsRepository>();

            AdminController target = new AdminController(mock.Object);

            Product product = new Product { Name = "Test" };

            target.ModelState.AddModelError("error", "error");

            ActionResult result = target.Edit(product);

            mock.Verify(m => m.SaveProduct(It.IsAny<Product>()), Times.Never());

            Assert.IsInstanceOfType(result, typeof(ViewResult));
        }
Esempio n. 9
0
 public void Can_Delete_Valid_Games()
 {
     Game game = new Game { GameId = 2, Name = "Game2" };
     // Arrange
     Mock<IGameRepository> mock = new Mock<IGameRepository>();
     mock.Setup(m => m.Games).Returns(new List<Game>
     {
         new Game { GameId = 1, Name = "Game1"},
         new Game { GameId = 2, Name = "Game2"},
         new Game { GameId = 3, Name = "Game3"},
         new Game { GameId = 4, Name = "Game4"},
         new Game { GameId = 5, Name = "Game5"}
     });
     AdminController ctrl = new AdminController(mock.Object);
     //Act
     ctrl.Delete(game.GameId);
     mock.Verify(m => m.DeleteGame(game.GameId));
 }
Esempio n. 10
0
        public void Cannot_Save_Invalid_Changes()
        {
            // Arrange
              Mock<IGameRepository> mock = new Mock<IGameRepository>();

            AdminController controller = new AdminController(mock.Object);

            Game game = new Game { Name = "Test" };

            controller.ModelState.AddModelError("error", "error");

            // Act
            ActionResult result = controller.Edit(game);

            // Assert appeal to the repository does not produce
            mock.Verify(m => m.SaveGame(It.IsAny<Game>()), Times.Never());

            Assert.IsInstanceOfType(result, typeof(ViewResult));
        }
Esempio n. 11
0
        public void Can_Edit_Game()
        {
            // Arrange
            Mock<IGameRepository> mock = new Mock<IGameRepository>();
            mock.Setup(m => m.Games).Returns(new List<Game>
            {
                new Game { GameId = 1, Name = "Game1"},
                new Game { GameId = 2, Name = "Game2"},
                new Game { GameId = 3, Name = "Game3"},
                new Game { GameId = 4, Name = "Game4"},
                new Game { GameId = 5, Name = "Game5"}
            });
            AdminController controller = new AdminController(mock.Object);

            // Act
            Game game1 = controller.Edit(1).ViewData.Model as Game;
            Game game2 = controller.Edit(2).ViewData.Model as Game;
            Game game3 = controller.Edit(3).ViewData.Model as Game;

            //Assert
            Assert.AreEqual(1, game1.GameId);
            Assert.AreEqual(2, game2.GameId);
            Assert.AreEqual(3, game3.GameId);
        }
Esempio n. 12
0
        public void Can_Save_Valid_Changes()
        {
            // Arrange
            Mock<IGameRepository> mock = new Mock<IGameRepository>();
            AdminController controller = new AdminController(mock.Object);
            Game game = new Game { Name = "Test" };

            // Act
            ActionResult result = controller.Edit(game);
            mock.Verify(m => m.SaveGame(game));
            // Assert
            Assert.IsNotInstanceOfType(result, typeof(ViewResult));
        }
Esempio n. 13
0
        public void Can_Save_Valid_Changes()
        {
            Mock<IProductsRepository> mock = new Mock<IProductsRepository>();

            AdminController target = new AdminController(mock.Object);

            Product product = new Product { Name = "Test" };

            ActionResult result = target.Edit(product);

            mock.Verify(m => m.SaveProduct(product));
            Assert.IsNotInstanceOfType(result, typeof(ViewResult));
        }
Esempio n. 14
0
        public void Can_Delete_Valid_Products()
        {
            // Arrange - create a Product
            Product prod = new Product { ProductID = 2, Name = "Test" };
            // 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"},prod,
                    new Product {ProductID = 3, Name = "P3"},
                });
            // Arrange - create the controller
            AdminController 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.ProductID));
        }
Esempio n. 15
0
 public void Can_Edit_Product()
 {
     // 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"},
         new Product {ProductID = 2, Name = "P2"},
         new Product {ProductID = 3, Name = "P3"},
         });
     // Arrange - create the controller
     AdminController target = new AdminController(mock.Object);
     // Act
     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
     Assert.AreEqual(1, p1.ProductID);
     Assert.AreEqual(2, p2.ProductID);
     Assert.AreEqual(3, p3.ProductID);
 }
Esempio n. 16
0
        public void Index_Contains_All_Games()
        {
            // Arrange
            Mock<IGameRepository> mock = new Mock<IGameRepository>();
            mock.Setup(m => m.Games).Returns(new List<Game>
            {
                new Game { GameId = 1, Name = "Game1"},
                new Game { GameId = 2, Name = "Game2"},
                new Game { GameId = 3, Name = "Game3"},
                new Game { GameId = 4, Name = "Game4"},
                new Game { GameId = 5, Name = "Game5"}
            });
            AdminController controller = new AdminController(mock.Object);

            // Act
            List<Game> result = ((IEnumerable<Game>)controller.Index().
                ViewData.Model).ToList();

            // Assert
            Assert.AreEqual(result.Count(), 5);
            Assert.AreEqual("Game1", result[0].Name);
            Assert.AreEqual("Game2", result[1].Name);
            Assert.AreEqual("Game3", result[2].Name);
        }
Esempio n. 17
0
 public void Can_Save_Valid_Changes()
 {
     // Arrange - create mock repository
     Mock<IProductsRepository> mock = new Mock<IProductsRepository>();
     // Arrange - create the controller
     AdminController target = new AdminController(mock.Object);
     // Arrange - create a product
     Product product = new Product { Name = "Test" };
     // Act - try to save the product
     ActionResult result = target.Edit(product);
     // Assert - check that the repository was called
     mock.Verify(m => m.SaveProduct(product));
     // Assert - check the method result type
     Assert.IsNotInstanceOfType(result, typeof(ViewResult));
 }
Esempio n. 18
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 = 1, Name = "P2"},
            new Product{ProductID = 1, Name = "P3"},
            });

            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);
        }
Esempio n. 19
0
 public void Cannot_Save_Invalid_Changes()
 {
     // Arrange - create mock repository
     Mock<IProductsRepository> mock = new Mock<IProductsRepository>();
     // Arrange - create the controller
     AdminController target = new AdminController(mock.Object);
     // Arrange - create a product
     Product 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);
     // 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));
 }