public void Create_HappyPath_ReturnsViewResult() { var gameController = CreateGameController(); GameModel gameModel = new GameModel(); UserShipsLocationViewModel userShipsLocationVM = new UserShipsLocationViewModel(); var result = gameController.Create(gameModel, userShipsLocationVM); Assert.IsAssignableFrom <ViewResult>(result); }
public void CheckShips_ServiceProviderThrowsException_ExceptionDoesNotPropagate() { mockServiceProvider.Setup(x => x.GetService(It.IsAny <Type>())).Throws <Exception>(); var gameController = CreateGameController(); UserShipsLocationViewModel userShipsLocationVM = null; Action action = () => gameController.CheckShips(userShipsLocationVM); Assert.DoesNotThrow(action.Invoke); }
public void Create_GameServiceThrowsException_ReturnsRedirectToActionResult() { mockServiceProvider.Setup(x => x.GetService(It.IsAny <Type>())).Throws <Exception>(); var gameController = CreateGameController(); GameModel gameModel = null; UserShipsLocationViewModel userShipsLocationVM = null; var result = gameController.Create(gameModel, userShipsLocationVM); Assert.IsAssignableFrom <RedirectToActionResult>(result); }
public void CheckShips_HappyPath_ReturnsRedirectToActionResult() { var gameController = CreateGameController(); mockGameService.Setup(x => x.GetShipsPositions(It.IsAny <IList <ShipLayout> >())).Returns(It.IsAny <bool[]>()); UserShipsLocationViewModel userShipsLocationVM = null; var result = gameController.CheckShips(userShipsLocationVM); Assert.IsAssignableFrom <RedirectToActionResult>(result); }
public void Create_GameServiceThrowsException_GameServiceRemovesGame() { var gameController = CreateGameController(); mockGameService.Setup(x => x.InitializeGame(It.IsAny <IList <ShipLayout> >(), It.IsAny <IDifficultyLevel>())).Throws <Exception>(); GameModel gameModel = new GameModel(); UserShipsLocationViewModel userShipsLocationVM = new UserShipsLocationViewModel(); var result = gameController.Create(gameModel, userShipsLocationVM); mockGameService.Verify(x => x.RemoveGame(gameModel.GameGuid), Times.Once); }
public void Create_ModelIsNotValid_ReturnsViewResult() { var gameController = CreateGameController(); gameController.ModelState.AddModelError("errorKey", "errorMessage"); GameModel gameModel = null; UserShipsLocationViewModel userShipsLocationVM = null; var result = gameController.Create(gameModel, userShipsLocationVM); Assert.IsAssignableFrom <ViewResult>(result); }
public void Create_GameServiceThrowsException_MessageToUserIsSavedToTempData() { var gameController = CreateGameController(); mockGameService.Setup(x => x.InitializeGame(It.IsAny <IList <ShipLayout> >(), It.IsAny <IDifficultyLevel>())).Throws <Exception>(); GameModel gameModel = new GameModel(); UserShipsLocationViewModel userShipsLocationVM = new UserShipsLocationViewModel(); var result = gameController.Create(gameModel, userShipsLocationVM); var messageToUser = gameController.TempData[nameof(UserCommunicationViewModel.MessageToUser)] as string; Assert.IsNotNull(messageToUser, "Message to user should be saved in TempData"); }
public void CheckShips_GameServiceThrowsException_MessageToUserIsSavedToTempData() { var gameController = CreateGameController(); var exception = new Exception("TestException"); mockGameService.Setup(x => x.GetShipsPositions(It.IsAny <IList <ShipLayout> >())).Throws(exception); UserShipsLocationViewModel userShipsLocationVM = new UserShipsLocationViewModel(); gameController.CheckShips(userShipsLocationVM); var messageToUser = gameController.TempData[nameof(UserCommunicationViewModel.MessageToUser)] as string; Assert.IsNotNull(messageToUser, "Message to user should be saved in TempData"); Assert.That(messageToUser.Contains(exception.Message), "Message to user should contain exception message"); }
public void Create_ModelIsNotValid_AddsMessageToTempData() { var gameController = CreateGameController(); gameController.ModelState.AddModelError("errorKey", "errorMessage"); GameModel gameModel = null; UserShipsLocationViewModel userShipsLocationVM = null; var result = gameController.Create(gameModel, userShipsLocationVM); Assert.IsAssignableFrom <ViewResult>(result); var messageToUser = gameController.TempData[nameof(UserCommunicationViewModel.MessageToUser)] as string; Assert.IsNotNull(messageToUser, "Message to user should be saved in TempData"); }
public void Index_TempDataContainsShipsFields_AddShipFieldsToModel() { var gameController = CreateGameController(); GameModel gameModel = new GameModel(); bool[] shipsFields = new[] { false, true }; var userShipsLocationVM = new UserShipsLocationViewModel() { ShipsFields = shipsFields }; var serializedVm = JsonConvert.SerializeObject(userShipsLocationVM); gameController.TempData[nameof(UserShipsLocationViewModel)] = serializedVm; gameController.Index(gameModel); Assert.AreEqual(shipsFields, gameModel.ShipsFields); }
public IActionResult CheckShips(UserShipsLocationViewModel userShipsLocationVM) { try { var shipLayouts = userShipsLocationVM.ShipsLayouts; var gameService = _serviceProvider.GetService <IGameService>(); var fields = gameService.GetShipsPositions(shipLayouts); userShipsLocationVM.ShipsFields = fields; } catch (Exception e) { _logger.LogError(e, $"General error in {nameof(CheckShips)} method"); TempData[nameof(UserCommunicationViewModel.MessageToUser)] = "Error: " + e.Message; } TempData.AddSerializedObject(userShipsLocationVM, nameof(UserShipsLocationViewModel)); return(RedirectToAction(nameof(Index))); }
public void CheckShips_HappyPath_UserShipLocationIsSavedToTempData() { var gameController = CreateGameController(); var shipFields = new[] { false, true }; mockGameService.Setup(x => x.GetShipsPositions(It.IsAny <IList <ShipLayout> >())).Returns(shipFields); UserShipsLocationViewModel userShipsLocationVM = new UserShipsLocationViewModel(); gameController.CheckShips(userShipsLocationVM); var serializedShipsLocation = gameController.TempData[nameof(UserShipsLocationViewModel)] as string; Assert.IsNotNull(serializedShipsLocation, "Player ships location should be saved in TempData"); var deserializedUserShipsLocationVM = JsonConvert.DeserializeObject <UserShipsLocationViewModel>(serializedShipsLocation); Assert.AreEqual(shipFields, deserializedUserShipsLocationVM.ShipsFields, "Ships locations should be the same"); }
public void Create_GameServiceThrowsException_ShipsLocationIsSavedToTempData() { var gameController = CreateGameController(); var shipFields = new[] { false, true }; mockGameService.Setup(x => x.InitializeGame(It.IsAny <IList <ShipLayout> >(), It.IsAny <IDifficultyLevel>())).Throws <Exception>(); GameModel gameModel = new GameModel(); UserShipsLocationViewModel userShipsLocationVM = new UserShipsLocationViewModel() { ShipsFields = shipFields }; var result = gameController.Create(gameModel, userShipsLocationVM); var serializedShipsLocation = gameController.TempData[nameof(UserShipsLocationViewModel)] as string; Assert.IsNotNull(serializedShipsLocation, "Player ships location should be saved in TempData"); var deserializedUserShipsLocationVM = JsonConvert.DeserializeObject <UserShipsLocationViewModel>(serializedShipsLocation); Assert.AreEqual(shipFields, deserializedUserShipsLocationVM.ShipsFields, "Ships locations should be the same"); }
public IActionResult Create(GameModel gameModel, UserShipsLocationViewModel userShipsLocationVM) { // Guid has not been created yet ModelState.ClearValidationState(nameof(GameModel.GameGuid)); ModelState.MarkFieldSkipped(nameof(GameModel.GameGuid)); if (!ModelState.IsValid) { _logger.LogError($"Model was not validated in {nameof(Create)} method"); TempData[nameof(UserCommunicationViewModel.MessageToUser)] = WarningGameRestarted; return(View(nameof(Index), gameModel)); } IGameService gameService = null; try { gameService = _serviceProvider.GetService <IGameService>(); gameModel.Game = gameService.InitializeGame(userShipsLocationVM.ShipsLayouts, new DifficultyLevelEasy()); gameModel.GameGuid = gameService.CurrentGameGuid; return(View("GameProgress", gameModel)); } catch (Exception e) { _logger.LogError(e, $"General error in {nameof(Create)} method"); if (gameService != null) { gameService.RemoveGame(gameModel.GameGuid); } TempData[nameof(UserCommunicationViewModel.MessageToUser)] = "Error: " + e.Message; TempData.AddSerializedObject(userShipsLocationVM, nameof(UserShipsLocationViewModel)); return(RedirectToAction(nameof(Index))); } }