public async Task EditPost_WhenIdEqualsLeagueIdAndModelStateIsValidAndDbUpdateConcurrencyExceptionIsCaughtAndLeagueWithIdExists_ShouldRethrowException() { // Arrange var leaguesIndexViewModel = A.Fake <ILeaguesIndexViewModel>(); var leaguesDetailsViewModel = A.Fake <ILeaguesDetailsViewModel>(); var leagueRepository = A.Fake <ILeagueRepository>(); A.CallTo(() => leagueRepository.Update(A <League> .Ignored)).Throws <DbUpdateConcurrencyException>(); A.CallTo(() => leagueRepository.LeagueExists(A <int> .Ignored)).Returns(true); var sharedRepository = A.Fake <ISharedRepository>(); var testController = new LeaguesController(leaguesIndexViewModel, leaguesDetailsViewModel, leagueRepository, sharedRepository); int id = 1; var league = new League { ID = 1 }; // Act var func = new Func <Task <IActionResult> >(async() => await testController.Edit(id, league)); // Assert await func.ShouldThrowAsync <DbUpdateConcurrencyException>(); }
public async Task EditPost_WhenIdEqualsLeagueIdAndModelStateIsNotValid_ShouldReturnLeagueEditView() { // Arrange var leaguesIndexViewModel = A.Fake <ILeaguesIndexViewModel>(); var leaguesDetailsViewModel = A.Fake <ILeaguesDetailsViewModel>(); var leagueRepository = A.Fake <ILeagueRepository>(); var sharedRepository = A.Fake <ISharedRepository>(); var testController = new LeaguesController(leaguesIndexViewModel, leaguesDetailsViewModel, leagueRepository, sharedRepository); int id = 1; var league = new League { ID = 1 }; testController.ModelState.AddModelError("LongName", "Please enter a long name."); // Act var result = await testController.Edit(id, league); // Assert A.CallTo(() => leagueRepository.Update(A <League> .Ignored)).MustNotHaveHappened(); A.CallTo(() => sharedRepository.SaveChangesAsync()).MustNotHaveHappened(); result.ShouldBeOfType <ViewResult>(); ((ViewResult)result).Model.ShouldBe(league); }
public async Task EditPost_WhenIdEqualsLeagueIdAndModelStateIsValidAndDbUpdateConcurrencyExceptionIsNotCaught_ShouldUpdateLeagueInDataStoreAndRedirectToIndexView() { // Arrange var leaguesIndexViewModel = A.Fake <ILeaguesIndexViewModel>(); var leaguesDetailsViewModel = A.Fake <ILeaguesDetailsViewModel>(); var leagueRepository = A.Fake <ILeagueRepository>(); var sharedRepository = A.Fake <ISharedRepository>(); var testController = new LeaguesController(leaguesIndexViewModel, leaguesDetailsViewModel, leagueRepository, sharedRepository); int id = 1; var league = new League { ID = 1 }; // Act var result = await testController.Edit(id, league); // Assert A.CallTo(() => leagueRepository.Update(league)).MustHaveHappenedOnceExactly(); A.CallTo(() => sharedRepository.SaveChangesAsync()).MustHaveHappenedOnceExactly(); result.ShouldBeOfType <RedirectToActionResult>(); ((RedirectToActionResult)result).ActionName.ShouldBe <string>(nameof(testController.Index)); }
public async Task EditPost_WhenIdEqualsLeagueIdAndModelStateIsValidAndDbUpdateConcurrencyExceptionIsCaughtAndLeagueWithIdDoesNotExist_ShouldReturnNotFound() { // Arrange var leaguesIndexViewModel = A.Fake <ILeaguesIndexViewModel>(); var leaguesDetailsViewModel = A.Fake <ILeaguesDetailsViewModel>(); var leagueRepository = A.Fake <ILeagueRepository>(); A.CallTo(() => leagueRepository.Update(A <League> .Ignored)).Throws <DbUpdateConcurrencyException>(); A.CallTo(() => leagueRepository.LeagueExists(A <int> .Ignored)).Returns(false); var sharedRepository = A.Fake <ISharedRepository>(); var testController = new LeaguesController(leaguesIndexViewModel, leaguesDetailsViewModel, leagueRepository, sharedRepository); int id = 1; var league = new League { ID = 1 }; // Act var result = await testController.Edit(id, league); // Assert result.ShouldBeOfType <NotFoundResult>(); }
public async Task EditGet_WhenIdIsNotNullAndLeagueFound_ShouldReturnLeagueEditView() { // Arrange var leaguesIndexViewModel = A.Fake <ILeaguesIndexViewModel>(); var leaguesDetailsViewModel = A.Fake <ILeaguesDetailsViewModel>(); var leagueRepository = A.Fake <ILeagueRepository>(); League?league = new League(); A.CallTo(() => leagueRepository.GetLeagueAsync(A <int> .Ignored)).Returns(league); var sharedRepository = A.Fake <ISharedRepository>(); var testController = new LeaguesController(leaguesIndexViewModel, leaguesDetailsViewModel, leagueRepository, sharedRepository); int?id = 0; // Act var result = await testController.Edit(id); // Assert A.CallTo(() => leagueRepository.GetLeagueAsync(id.Value)).MustHaveHappenedOnceExactly(); result.ShouldBeOfType <ViewResult>(); ((ViewResult)result).Model.ShouldBe(league); }
public void CheckFunctionEdit() { // Add an initial League object to the database prior to editing League league = new League(); league.LeagueId = 1; league.Year = "2007"; databaseContext.League.Add(league); databaseContext.SaveChanges(); // Create the controller and call the Edit method, passing the new League object LeaguesController controller = new LeaguesController(databaseContext); league.Year = "2019"; var resultTask = controller.Edit(1, league); resultTask.Wait(); IActionResult result = resultTask.Result as IActionResult; // Fetch the league object from the database and check if it's value has been updated. var postLeagueTask = databaseContext.League.FirstOrDefaultAsync(l => l.LeagueId == 1); postLeagueTask.Wait(); var postLeague = postLeagueTask.Result; Assert.AreEqual(postLeague.Year, "2019"); }
public void ValidResponseEdit() { // Create the League controller LeaguesController controller = new LeaguesController(databaseContext); // Call the Edit page and wait for a response var resultTask = controller.Edit(1); resultTask.Wait(); IActionResult result = resultTask.Result as IActionResult; // Check that the controller returned a valid response Assert.IsNotNull(result); }
public async Task EditGet_WhenIdIsNull_ShouldReturnNotFound() { // Arrange var leaguesIndexViewModel = A.Fake <ILeaguesIndexViewModel>(); var leaguesDetailsViewModel = A.Fake <ILeaguesDetailsViewModel>(); var leagueRepository = A.Fake <ILeagueRepository>(); var sharedRepository = A.Fake <ISharedRepository>(); var testController = new LeaguesController(leaguesIndexViewModel, leaguesDetailsViewModel, leagueRepository, sharedRepository); int?id = null; // Act var result = await testController.Edit(id); // Assert result.ShouldBeOfType <NotFoundResult>(); }
public async Task EditPost_WhenIdDoesNotEqualLeagueId_ShouldReturnNotFound() { // Arrange var leaguesIndexViewModel = A.Fake <ILeaguesIndexViewModel>(); var leaguesDetailsViewModel = A.Fake <ILeaguesDetailsViewModel>(); var leagueRepository = A.Fake <ILeagueRepository>(); var sharedRepository = A.Fake <ISharedRepository>(); var testController = new LeaguesController(leaguesIndexViewModel, leaguesDetailsViewModel, leagueRepository, sharedRepository); int id = 0; var league = new League { ID = 1 }; // Act var result = await testController.Edit(id, league); // Assert result.ShouldBeOfType <NotFoundResult>(); }