public async Task EditPost_WhenIdEqualsGameIdAndModelStateIsValidAndDbConcurrencyExceptionIsCaughtAndGameWithIdExists_ShouldRethrowException() { // Arrange var gamesIndexViewModel = A.Fake <IGamesIndexViewModel>(); var gamesDetailsViewModel = A.Fake <IGamesDetailsViewModel>(); var gameService = A.Fake <IGameService>(); A.CallTo(() => gameService.EditGameAsync(A <Game> .Ignored, A <Game> .Ignored)) .Throws <DbUpdateConcurrencyException>(); var gameRepository = A.Fake <IGameRepository>(); A.CallTo(() => gameRepository.GameExists(An <int> .Ignored)).Returns(true); var teamRepository = A.Fake <ITeamRepository>(); var seasonRepository = A.Fake <ISeasonRepository>(); var sharedRepository = A.Fake <ISharedRepository>(); var testController = new GamesController(gamesIndexViewModel, gamesDetailsViewModel, gameService, gameRepository, teamRepository, seasonRepository, sharedRepository); int id = 1; var game = new Game { ID = 1 }; // Act var func = new Func <Task <IActionResult> >(async() => await testController.Edit(id, game)); // Assert await func.ShouldThrowAsync <DbUpdateConcurrencyException>(); }
public async Task EditPost_WhenIdEqualsGameIdAndModelStateIsValidAndDbConcurrencyExceptionIsNotCaught_ShouldUpdateGameInDataStoreAndRedirectToIndexView() { // Arrange var oldGame = new Game(); GamesController.OldGame = oldGame; var gamesIndexViewModel = A.Fake <IGamesIndexViewModel>(); var gamesDetailsViewModel = A.Fake <IGamesDetailsViewModel>(); var gameService = A.Fake <IGameService>(); var gameRepository = A.Fake <IGameRepository>(); var teamRepository = A.Fake <ITeamRepository>(); var seasonRepository = A.Fake <ISeasonRepository>(); var sharedRepository = A.Fake <ISharedRepository>(); var testController = new GamesController(gamesIndexViewModel, gamesDetailsViewModel, gameService, gameRepository, teamRepository, seasonRepository, sharedRepository); int id = 1; var game = new Game { ID = 1 }; // Act var result = await testController.Edit(id, game); // Assert A.CallTo(() => gameService.EditGameAsync(game, oldGame)).MustHaveHappenedOnceExactly(); A.CallTo(() => sharedRepository.SaveChangesAsync()).MustHaveHappenedOnceExactly(); result.ShouldBeOfType <RedirectToActionResult>(); ((RedirectToActionResult)result).ActionName.ShouldBe <string>(nameof(testController.Index)); }
public async Task EditPost_WhenIdEqualsGameIdAndModelStateIsValidAndDbConcurrencyExceptionIsCaughtAndGameWithIdDoesNotExist_ShouldReturnNotFound() { // Arrange var gamesIndexViewModel = A.Fake <IGamesIndexViewModel>(); var gamesDetailsViewModel = A.Fake <IGamesDetailsViewModel>(); var gameService = A.Fake <IGameService>(); A.CallTo(() => gameService.EditGameAsync(A <Game> .Ignored, A <Game> .Ignored)) .Throws <DbUpdateConcurrencyException>(); var gameRepository = A.Fake <IGameRepository>(); A.CallTo(() => gameRepository.GameExists(An <int> .Ignored)).Returns(false); var teamRepository = A.Fake <ITeamRepository>(); var seasonRepository = A.Fake <ISeasonRepository>(); var sharedRepository = A.Fake <ISharedRepository>(); var testController = new GamesController(gamesIndexViewModel, gamesDetailsViewModel, gameService, gameRepository, teamRepository, seasonRepository, sharedRepository); int id = 1; var game = new Game { ID = 1 }; // Act var result = await testController.Edit(id, game); // Assert result.ShouldBeOfType <NotFoundResult>(); }
public async void TestEdit_InvalidGame_ShouldFail(string value) { // Arrange GamesController controller = new GamesController(_context, _webHostEnvironment); int gameId = int.Parse(value); // Act Game replayGame = await _context.Game .FirstOrDefaultAsync(a => a.GameId == gameId); replayGame.GameName = ""; try { var result = await controller.Edit(replayGame.GameId, replayGame); // Assert Assert.IsType <ViewResult>(result); ViewResult viewResult = (ViewResult)result; Assert.NotNull(viewResult.ViewData.ModelState); Assert.NotEmpty(viewResult.ViewData.ModelState.Keys); foreach (string item in viewResult.ViewData.ModelState.Keys) { Assert.Equal("", item); } } catch (Exception ex) { Assert.Equal("Xunit.Sdk.IsTypeException", ex.GetType().ToString()); } }
public async Task EditPost_WhenIdEqualsGameIdAndModelStateIsNotValid_ShouldReturnEditGameView() { // Arrange var gamesIndexViewModel = A.Fake <IGamesIndexViewModel>(); var gamesDetailsViewModel = A.Fake <IGamesDetailsViewModel>(); var gameService = A.Fake <IGameService>(); var gameRepository = A.Fake <IGameRepository>(); var teamRepository = A.Fake <ITeamRepository>(); var seasonRepository = A.Fake <ISeasonRepository>(); var sharedRepository = A.Fake <ISharedRepository>(); var testController = new GamesController(gamesIndexViewModel, gamesDetailsViewModel, gameService, gameRepository, teamRepository, seasonRepository, sharedRepository); int id = 1; var game = new Game { ID = 1 }; testController.ModelState.AddModelError("Season", "Please enter a season."); // Act var result = await testController.Edit(id, game); // Assert result.ShouldBeOfType <ViewResult>(); ((ViewResult)result).Model.ShouldBe(game); }
public async Task EditGet_WhenIdIsNotNullAndGameNotFound_ShouldReturnNotFound() { // Arrange var gamesIndexViewModel = A.Fake <IGamesIndexViewModel>(); var gamesDetailsViewModel = A.Fake <IGamesDetailsViewModel>(); var gameService = A.Fake <IGameService>(); var gameRepository = A.Fake <IGameRepository>(); int? id = 1; Game?game = null; A.CallTo(() => gameRepository.GetGameAsync(id.Value)).Returns(game); var teamRepository = A.Fake <ITeamRepository>(); var seasonRepository = A.Fake <ISeasonRepository>(); var sharedRepository = A.Fake <ISharedRepository>(); var testController = new GamesController(gamesIndexViewModel, gamesDetailsViewModel, gameService, gameRepository, teamRepository, seasonRepository, sharedRepository); // Act var result = await testController.Edit(id); // Assert result.ShouldBeOfType <NotFoundResult>(); }
public async Task Edit_IdAsParam_ReturnIndexView() { var optionsBuilder = new DbContextOptionsBuilder <ApplicationDbContext>(); optionsBuilder.UseInMemoryDatabase("test"); var _dbContext = new ApplicationDbContext(optionsBuilder.Options); var c = new GamesController(_dbContext, _hostingEnv); Games game = new Games(); game.Id = 1; game.Title = "test"; game.Genre = "rock"; game.Description = "loud stuff"; game.Publisher = "da"; game.Developer = "EA"; game.ESRB_Ratings = "lalaalaa"; game.ReleaseDate = new System.DateTime(); game.Price = 2; game.GameLink = "dsa"; game.Available = "dsadas"; game.EntryDate = new System.DateTime(); var result = await c.Edit(1); Assert.IsInstanceOfType(result, typeof(ViewResult)); }
public void GamesControllerEdit() { var gamesController = new GamesController(null); var result = gamesController.Edit(null); Assert.IsNotNull(result); }
public void EditTest1() { // Arrange GamesController controller = new GamesController(); // Act var result = controller.Edit(1); // Assert Assert.IsNotNull(result); }
public void EditRedirectsToTheGameIndexView() { var id = 5; var mockGamesContext = new MockGamesContext(); var controller = new GamesController(mockGamesContext); var result = controller.Edit(id); Assert.IsType <RedirectToRouteResult>(result); Assert.Equal("Index", result.RouteValues["action"]); Assert.Equal("Game", result.RouteValues["controller"]); Assert.Equal(id, result.RouteValues["id"]); }
public void EditGet_ExceptionCaught_RethrowsException() { // Arrange var controller = new GamesController(_service, _sharedService); int?id = 1; A.CallTo(() => _service.FindEntityAsync(A <int> .Ignored, null)).Throws <Exception>(); // Act // Assert Assert.ThrowsAsync <Exception>(async() => await controller.Edit(id)); }
public async Task EditGet_IdNull_ReturnsBadHttpRequest() { // Arrange var controller = new GamesController(_service, _sharedService); int?id = null; // Act var result = await controller.Edit(id); // Assert Assert.IsInstanceOf <HttpStatusCodeResult>(result); Assert.AreEqual((int)HttpStatusCode.BadRequest, (result as HttpStatusCodeResult).StatusCode); }
public async Task Edit_IdAndInvalidGameObjectAsParam_ReturnIndexView() { var optionsBuilder = new DbContextOptionsBuilder <ApplicationDbContext>(); optionsBuilder.UseInMemoryDatabase("test"); var _dbContext = new ApplicationDbContext(optionsBuilder.Options); var c = new GamesController(_dbContext, _hostingEnv); Games game = new Games(); var result = await c.Edit(1); Assert.IsInstanceOfType(result, typeof(ViewResult)); }
public async Task IsAbleToUpdateGameAsync() { await InsertGame(); var existingGame = GetGamesList()[0]; var gameUpdated = _repositoryStub.Find(existingGame.Id).Result; gameUpdated.Name = "My Game's Name Updated Test"; var updateResult = _sut.Edit(gameUpdated.Id, gameUpdated); var updatedGame = _repositoryStub.Find(existingGame.Id).Result; Assert.AreEqual(gameUpdated.Name, GetGamesList()[0].Name); }
public async void GamesController_Edit_Game() { //Arrange var mockGame = new Game() { GameId = 1 }; var mockGameRepository = new MockGameRepository().MockGetGameByID(mockGame); var controller = new GamesController(mockGameRepository.Object); //act var result = await controller.Edit(1); //Assert Assert.IsAssignableFrom <ActionResult <Game> >(result); mockGameRepository.VerifyGetGameByID(Times.Once()); }
public void Edit_UpdateGame_ServiceMethodWereCalled() { //Arrange Guid guid1 = Guid.NewGuid(); string GameName1 = "Name1"; GameViewModel Game = new GameViewModel { GameGuid = guid1, Name = GameName1 }; //Service.Setup(x => x.GetGames()).Returns(Game); GamesController controller = new GamesController(Service.Object, Mapper); //Act var result = controller.Edit(Game, null, null) as ViewResult; //Assert Service.Verify(x => x.EditGame(It.IsAny <GameDTO>())); }
public async Task EditGet_IdNotNullAndGameViewModelNull_ReturnsHttpNotFound() { // Arrange var controller = new GamesController(_service, _sharedService); int?id = 1; GameViewModel game = null; A.CallTo(() => _service.FindEntityAsync(A <int> .Ignored, null)).Returns(game); // Act var result = await controller.Edit(id); // Assert A.CallTo(() => _service.FindEntityAsync((int)id, null)).MustHaveHappenedOnceExactly(); Assert.IsInstanceOf <HttpNotFoundResult>(result); }
public void EditPost_ExceptionCaught_RethrowsException() { // Arrange var controller = new GamesController(_service, _sharedService); controller.ModelState.AddModelError("GuestScore", "Guest score is required."); var gameViewModel = new GameViewModel { SeasonID = 2017, GuestName = "Guest", HostName = "Host" }; A.CallTo(() => _sharedService.GetSeasonsOrderedAsync(null)).Throws <Exception>(); // Act // Assert Assert.ThrowsAsync <Exception>(async() => await controller.Edit(gameViewModel)); }
public async Task EditGet_WhenIdIsNull_ShouldReturnNotFound() { // Arrange var gamesIndexViewModel = A.Fake <IGamesIndexViewModel>(); var gamesDetailsViewModel = A.Fake <IGamesDetailsViewModel>(); var gameService = A.Fake <IGameService>(); var gameRepository = A.Fake <IGameRepository>(); var teamRepository = A.Fake <ITeamRepository>(); var seasonRepository = A.Fake <ISeasonRepository>(); var sharedRepository = A.Fake <ISharedRepository>(); var testController = new GamesController(gamesIndexViewModel, gamesDetailsViewModel, gameService, gameRepository, teamRepository, seasonRepository, sharedRepository); int?id = null; // Act var result = await testController.Edit(id); // Assert result.ShouldBeOfType <NotFoundResult>(); }
public async Task EditPost_ModelStateValid() { // Arrange var controller = new GamesController(_service, _sharedService); var gameViewModel = new GameViewModel { SeasonID = 2017 }; // Act var result = await controller.Edit(gameViewModel); // Assert A.CallTo(() => _service.EditGame(A <GameViewModel> .Ignored, gameViewModel, null)) .MustHaveHappenedOnceExactly(); Assert.IsInstanceOf <RedirectToRouteResult>(result); var resultAsRedirectToRouteResult = result as RedirectToRouteResult; Assert.AreEqual("Index", resultAsRedirectToRouteResult.RouteValues["action"]); Assert.AreEqual(gameViewModel.SeasonID, resultAsRedirectToRouteResult.RouteValues["seasonID"]); }
public async Task EditPost_WhenIdDoesNotEqualGameId_ShouldReturnNotFound() { // Arrange var gamesIndexViewModel = A.Fake <IGamesIndexViewModel>(); var gamesDetailsViewModel = A.Fake <IGamesDetailsViewModel>(); var gameService = A.Fake <IGameService>(); var gameRepository = A.Fake <IGameRepository>(); var teamRepository = A.Fake <ITeamRepository>(); var seasonRepository = A.Fake <ISeasonRepository>(); var sharedRepository = A.Fake <ISharedRepository>(); var testController = new GamesController(gamesIndexViewModel, gamesDetailsViewModel, gameService, gameRepository, teamRepository, seasonRepository, sharedRepository); int id = 0; var game = new Game { ID = 1 }; // Act var result = await testController.Edit(id, game); // Assert result.ShouldBeOfType <NotFoundResult>(); }
public async Task EditGet_WhenIdIsNotNullAndGameFound_ShouldShowGameEditView() { // Arrange int selectedSeasonYear = 1920; GamesController.SelectedSeasonYear = selectedSeasonYear; var gamesIndexViewModel = A.Fake <IGamesIndexViewModel>(); var gamesDetailsViewModel = A.Fake <IGamesDetailsViewModel>(); var gameService = A.Fake <IGameService>(); var gameRepository = A.Fake <IGameRepository>(); int? id = 1; Game?game = new Game { SeasonYear = 1920, Week = 1 }; A.CallTo(() => gameRepository.GetGameAsync(id.Value)).Returns(game); var teamRepository = A.Fake <ITeamRepository>(); var seasonRepository = A.Fake <ISeasonRepository>(); var seasons = new List <Season> { new Season { Year = 1920, NumOfWeeksScheduled = 3 }, }; var selectedSeason = seasons.FirstOrDefault(s => s.Year == selectedSeasonYear); A.CallTo(() => seasonRepository.GetSeasonsAsync()).Returns(seasons); var sharedRepository = A.Fake <ISharedRepository>(); var testController = new GamesController(gamesIndexViewModel, gamesDetailsViewModel, gameService, gameRepository, teamRepository, seasonRepository, sharedRepository); // Act var result = await testController.Edit(id); // Assert A.CallTo(() => seasonRepository.GetSeasonsAsync()).MustHaveHappenedOnceExactly(); Assert.IsType <SelectList>(testController.ViewBag.Seasons); var seasonsSelectList = (SelectList)testController.ViewBag.Seasons; seasonsSelectList.Items.ShouldBe(seasons.OrderByDescending(s => s.Year)); seasonsSelectList.DataValueField.ShouldBe <string>("Year"); seasonsSelectList.DataTextField.ShouldBe <string>("Year"); seasonsSelectList.SelectedValue.ShouldBe(game.SeasonYear); Assert.IsType <SelectList>(testController.ViewBag.Weeks); var weeksSelectList = (SelectList)testController.ViewBag.Weeks; weeksSelectList.Items.ShouldBeEquivalentTo(new List <int?> { 1, 2, 3 }); weeksSelectList.SelectedValue.ShouldBe(game.Week); GamesController.OldGame.ShouldBe(game); result.ShouldBeOfType <ViewResult>(); ((ViewResult)result).Model.ShouldBe(game); }
public async Task EditPost_ModelStateNotValid() { // Arrange var controller = new GamesController(_service, _sharedService); controller.ModelState.AddModelError("GuestScore", "Guest score is required."); var gameViewModel = new GameViewModel { SeasonID = 2017, GuestName = "Guest", HostName = "Host" }; var seasons = new List <SeasonViewModel>(); A.CallTo(() => _sharedService.GetSeasonsOrderedAsync(null)).Returns(seasons); GamesService.SelectedWeek = new WeekViewModel(1); var teams = new List <TeamViewModel>(); A.CallTo(() => _service.GetTeamsAsync(null)).Returns(teams); // Act var result = await controller.Edit(gameViewModel); // Assert A.CallTo(() => _sharedService.GetSeasonsOrderedAsync(null)).MustHaveHappenedOnceExactly(); Assert.IsInstanceOf <ViewResult>(result); var resultAsViewResult = result as ViewResult; var viewBag = resultAsViewResult.ViewBag; var viewBagSeasonID = viewBag.SeasonID; Assert.IsInstanceOf <SelectList>(viewBagSeasonID); Assert.AreSame(seasons, viewBagSeasonID.Items); Assert.AreEqual("ID", viewBagSeasonID.DataValueField); Assert.AreEqual("ID", viewBagSeasonID.DataTextField); Assert.AreEqual(gameViewModel.SeasonID, viewBagSeasonID.SelectedValues[0]); A.CallTo(() => _service.GetTeamsAsync(null)).MustHaveHappenedOnceExactly(); var viewBagGuestName = viewBag.GuestName; Assert.IsInstanceOf <SelectList>(viewBagGuestName); Assert.AreSame(teams, viewBagGuestName.Items); Assert.AreEqual("Name", viewBagGuestName.DataValueField); Assert.AreEqual("Name", viewBagGuestName.DataTextField); Assert.AreEqual(gameViewModel.GuestName, viewBagGuestName.SelectedValues[0]); var viewBagHostName = viewBag.HostName; Assert.IsInstanceOf <SelectList>(viewBagHostName); Assert.AreSame(teams, viewBagHostName.Items); Assert.AreEqual("Name", viewBagHostName.DataValueField); Assert.AreEqual("Name", viewBagHostName.DataTextField); Assert.AreEqual(gameViewModel.HostName, viewBagHostName.SelectedValues[0]); Assert.AreSame(gameViewModel, resultAsViewResult.Model); }
public async Task EditGet_IdNotNullAndGameViewModelNotNull_ReturnsViewResult() { // Arrange var controller = new GamesController(_service, _sharedService); int?id = 1; var game = new GameViewModel { SeasonID = 2017, GuestName = "Guest", HostName = "Host" }; A.CallTo(() => _service.FindEntityAsync(A <int> .Ignored, null)).Returns(game); var seasons = new List <SeasonViewModel>(); A.CallTo(() => _sharedService.GetSeasonsOrderedAsync(null)).Returns(seasons); var teams = new List <TeamViewModel>(); A.CallTo(() => _service.GetTeamsAsync(null)).Returns(teams); // Act var result = await controller.Edit(id); // Assert A.CallTo(() => _service.FindEntityAsync((int)id, null)).MustHaveHappenedOnceExactly(); Assert.IsInstanceOf <ViewResult>(result); var viewBag = (result as ViewResult).ViewBag; A.CallTo(() => _sharedService.GetSeasonsOrderedAsync(null)).MustHaveHappenedOnceExactly(); var viewBagSeasonID = viewBag.SeasonID; Assert.IsInstanceOf <SelectList>(viewBagSeasonID); Assert.AreSame(seasons, viewBagSeasonID.Items); Assert.AreEqual("ID", viewBagSeasonID.DataValueField); Assert.AreEqual("ID", viewBagSeasonID.DataTextField); Assert.AreEqual(game.SeasonID, viewBagSeasonID.SelectedValues[0]); A.CallTo(() => _service.GetTeamsAsync(null)).MustHaveHappenedOnceExactly(); var viewBagGuestName = viewBag.GuestName; Assert.IsInstanceOf <SelectList>(viewBagGuestName); Assert.AreSame(teams, viewBagGuestName.Items); Assert.AreEqual("Name", viewBagGuestName.DataValueField); Assert.AreEqual("Name", viewBagGuestName.DataTextField); Assert.AreEqual(game.GuestName, viewBagGuestName.SelectedValues[0]); var viewBagHostName = viewBag.HostName; Assert.IsInstanceOf <SelectList>(viewBagHostName); Assert.AreSame(teams, viewBagHostName.Items); Assert.AreEqual("Name", viewBagHostName.DataValueField); Assert.AreEqual("Name", viewBagHostName.DataTextField); Assert.AreEqual(game.HostName, viewBagHostName.SelectedValues[0]); }