public void Ad_Can_Edit_Product() { // Arrange - create the mock repository Mock <IAdvertisementRepository> mock = new Mock <IAdvertisementRepository>(); mock.Setup(m => m.Advertisements).Returns(new Advertisement[] { new Advertisement { AdvertisementID = 1, CompanyName = "A1" }, new Advertisement { AdvertisementID = 2, CompanyName = "A2" }, new Advertisement { AdvertisementID = 3, CompanyName = "A3" } }.AsQueryable <Advertisement>()); // Arrange - create the controller AdminController target = new AdminController(null, mock.Object); // Act Advertisement p1 = GetViewModel <Advertisement>(target.AdEdit(1)); Advertisement p2 = GetViewModel <Advertisement>(target.AdEdit(2)); Advertisement p3 = GetViewModel <Advertisement>(target.AdEdit(3)); //Assert Assert.Equal(1, p1.AdvertisementID); Assert.Equal(2, p2.AdvertisementID); Assert.Equal(3, p3.AdvertisementID); }
public void Ad_Can_Save_Valid_Changes() { // Arrange - create mock repository Mock <IAdvertisementRepository> mock = new Mock <IAdvertisementRepository>(); // Arrange - create mock temp data Mock <ITempDataDictionary> tempData = new Mock <ITempDataDictionary>(); // Arrange - create the controller AdminController target = new AdminController(null, mock.Object) { TempData = tempData.Object }; // Arrange - create a advertisement Advertisement advertisement = new Advertisement { CompanyName = "Test" }; // Act - try to save the product IActionResult result = target.AdEdit(advertisement); // Assert - check that the repository was called mock.Verify(m => m.SaveAdvertisement(advertisement)); // Assert - check the result type is a redirection Assert.IsType <RedirectToActionResult>(result); Assert.Equal("AdIndex", (result as RedirectToActionResult).ActionName); }
public void Ad_Cannot_Edit_Nonexistent_Product() { // Arrange - create the mock repository Mock <IAdvertisementRepository> mock = new Mock <IAdvertisementRepository>(); mock.Setup(m => m.Advertisements).Returns(new Advertisement[] { new Advertisement { AdvertisementID = 1, CompanyName = "A1" }, new Advertisement { AdvertisementID = 2, CompanyName = "A2" }, new Advertisement { AdvertisementID = 3, CompanyName = "A3" } }.AsQueryable <Advertisement>()); // Arrange - create the controller AdminController target = new AdminController(null, mock.Object); // Act Advertisement result = GetViewModel <Advertisement>(target.AdEdit(4)); // Assert Assert.Null(result); }
public void Ad_Cannot_Save_Invalid_Changes() { // Arrange - create mock repository Mock <IAdvertisementRepository> mock = new Mock <IAdvertisementRepository>(); // Arrange - create the controller AdminController target = new AdminController(null, mock.Object); // Arrange - create a advertisement Advertisement advertisement = new Advertisement { CompanyName = "Test" }; // Arrange - add an error to the model state target.ModelState.AddModelError("error", "error"); // Act - try to save the advertisement IActionResult result = target.AdEdit(advertisement); // Assert - check that the repository was not called mock.Verify(m => m.SaveAdvertisement(It.IsAny <Advertisement>()), Times.Never()); // Assert - check the method result type Assert.IsType <ViewResult>(result); }