public void Test_NewGame() { //arrange GamesController controller = new GamesController( _gameService.Object, null, _genreService.Object, _platformTypeService.Object, _publisherService.Object); CreateGameViewModel game = new CreateGameViewModel() { Key = "key", Description = "description", GameId = 1, Name = "name", Genres = new List<int> { 1 }, PlatformTypes = new List<int>() { 1 }, Publishers = "name1" }; //act var result = controller.New(game) as ViewResult; //assert _gameService.Verify(s => s.CreateGame(It.IsAny<GameDTO>()), Times.Once()); }
public void Test_NewGame_Try_Add_Game_With_Empty_Field() { //arrange GamesController controller = new GamesController( _gameService.Object, null, _genreService.Object, _platformTypeService.Object, _publisherService.Object); CreateGameViewModel game = new CreateGameViewModel() { Key = "", Description = "description", GameId = 1, Name = "name", Genres = new List<int> { 1 }, PlatformTypes = new List<int> { 1 } }; //act controller.ViewData.ModelState.AddModelError("Key", "message"); var result = controller.New(game) as ViewResult; //assert Assert.IsFalse(controller.ViewData.ModelState.IsValid); Assert.AreEqual(1, result.ViewData.ModelState.Count); }
/// <summary> /// Get game view model from create view model with setted some fields /// </summary> /// <param name="game">Create game view model</param> /// <returns>Game view model</returns> private GameViewModel GetGameViewModel(CreateGameViewModel game) { GameViewModel model = game != null ? Mapper.Map<CreateGameViewModel, GameViewModel>( game ) : new GameViewModel(); model.Genres = new MultiSelectList( _genreService.GetAll() .Select(genre => new SelectListItem() {Text = genre.Name, Value = genre.GenreId.ToString()})); model.PlatformTypes = new MultiSelectList( _plarfotmTypeService.GetAll() .Select(pt => new SelectListItem() {Text = pt.Name, Value = pt.PlatformTypeId.ToString()})); model.Publishers = new SelectList( _publisherService.GetAll() .Select(p => new SelectListItem() {Value = p.CompanyName, Text = p.CompanyName})); //set some field if (game != null) { if (game.Genres != null) { foreach (int id in game.Genres) { model.Genres.Each( m => m.Selected = m.Value == id.ToString() ); } } if (game.PlatformTypes != null) { foreach (int id in game.PlatformTypes) { model.PlatformTypes.Each( m => m.Selected = m.Value == id.ToString() ); } } } return model; }
public void Test_NewGame_Selected_Genres() { //arrange GamesController controller = new GamesController( _gameService.Object, null, _genreService.Object, _platformTypeService.Object, _publisherService.Object); CreateGameViewModel game = new CreateGameViewModel() { Key = "key", Description = "description", GameId = 1, Name = "name", Genres = new List<int> { 1 }, PlatformTypes = new List<int> { 1 } }; //act controller.ViewData.ModelState.AddModelError("Key", "message"); var result = controller.New(game) as ViewResult; //assert Assert.IsInstanceOfType(result.Model, typeof(GameViewModel)); Assert.AreEqual(2, (result.Model as GameViewModel).PlatformTypes.Count()); }
public ActionResult New(CreateGameViewModel game) { if (ModelState.IsValid) { game.AddedToStoreDate = DateTime.UtcNow; GameDTO gameDTO = Mapper.Map<CreateGameViewModel, GameDTO>(game); gameDTO.Genres = _genreService.GetAll().Where(genre => game.Genres.Any(id => id == genre.GenreId)); gameDTO.PlatformTypes = _plarfotmTypeService.GetAll().Where(pt => game.PlatformTypes.Any(id => id == pt.PlatformTypeId)); gameDTO.Publisher = _publisherService.GetPublisherByCompanyName(game.Publishers); _gameService.CreateGame(gameDTO); return new RedirectResult("/games/"); } GameViewModel model = GetGameViewModel(game); return View(model); }