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 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);
 }
示例#4
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);
        }
示例#5
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);
        }
示例#6
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));
        }
示例#7
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));
        }
 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));
 }
示例#9
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));
        }
示例#10
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));
        }
示例#11
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));
 }
示例#12
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);
 }
示例#13
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));
 }