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)); }
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()); }