public void CannotReviewAprovedOrRejectedAdvert() { //Arrange // controller arranged from the controller fixture //Act AdvertReviewViewModel ad1 = new AdvertReviewViewModel() { Id = 1, Comments = "All ok in this advert. The comments should be at least 30 chars long.", Confirm = true, Status = AdvertStatus.Approved }; AdvertReviewViewModel ad2 = new AdvertReviewViewModel() { Id = 5, Comments = "All ok in this advert. The comments should be at least 30 chars long.", Confirm = true, Status = AdvertStatus.Approved }; var res1 = Controller.Advert(ad1); var res2 = Controller.Advert(ad2); //Assert // Edit was not called MockRepo.Verify(m => m.SaveAdvert(It.IsAny <Advert>()), Times.Never()); // Result is ViewResult and no RedirectToAction Assert.IsType <ViewResult>(res1); Assert.IsType <ViewResult>(res2); Assert.IsNotType <RedirectToActionResult>(res1); Assert.IsNotType <RedirectToActionResult>(res2); }
public void CanViewPendingAdverts() { //Arrange // controller arranged from the controller fixture //Act AdvertReviewViewModel a1 = GetViewModel <AdvertReviewViewModel>(Controller.Advert(7)); AdvertReviewViewModel a2 = GetViewModel <AdvertReviewViewModel>(Controller.Advert(8)); AdvertReviewViewModel a3 = GetViewModel <AdvertReviewViewModel>(Controller.Advert(9)); //Assert Assert.Equal(7, a1.Id); Assert.Equal(8, a2.Id); Assert.Equal(9, a3.Id); }
public IActionResult Advert(AdvertReviewViewModel advReview) { // check if confirm = true, otherwise modelstate is not valid if (!advReview.Confirm) { ModelState.AddModelError("Confirm", "You must confirm the review before submitting."); } var advert = advertRepository.Adverts. FirstOrDefault(adv => adv.Id == advReview.Id); if (advert != null && (advert.Status == AdvertStatus.Approved || advert.Status == AdvertStatus.Rejected)) { ModelState.AddModelError("Status", "This advert has already been reviewed"); } if (ModelState.IsValid && advert != null) { // Get application Id from current user var userId = HttpContext.User.Claims .FirstOrDefault(c => c.Type == "UniAccomodationId")?.Value; // update advert status and comments advert.Review(advReview.Status, advReview.Comments, Int32.Parse(userId)); advertRepository.SaveAdvert(advert); // add message as advert was reviewed TempData["Message"] = $"Advert {advert.Id} has been reviewed. Status: {advert.Status}"; // return to pending list (index action) return(RedirectToAction("Index")); } else { return(View("Advert", new AdvertReviewViewModel() { Id = advert.Id, Title = advert.Title, Description = advert.Description, MonthlyPrice = advert.MonthlyPrice, PhotoUrl = advert.PhotoUrl, Status = advert.Status, Comments = advert.Comments })); } }
public void CanReviewAdvert() { //Arrange // controller arranged from the controller fixture // repository arranged from the controller fixture //Act AdvertReviewViewModel advert = new AdvertReviewViewModel() { Id = 1, Comments = "All ok in this advert. The comments should be at least 30 chars long.", Confirm = true, Status = AdvertStatus.Pending }; IActionResult result = Controller.Advert(advert); //Assert // Edit was called once, saved an advert with id 7 MockRepo.Verify(m => m.SaveAdvert(It.Is <Advert>(a => a.Id == 1)), Times.Once()); // Result type is a redirection to index page Assert.IsType <RedirectToActionResult>(result); Assert.Equal("Index", (result as RedirectToActionResult).ActionName); }