public void Cannot_Save_Invalid_Changes() { Mock<IPlayerRepository> mockPlayer = new Mock<IPlayerRepository>(); Mock<IClubRepository> mockClubs = new Mock<IClubRepository>(); Mock<IPositionRepository> mockPositions = new Mock<IPositionRepository>(); Mock<IStatRepository> mockStats = new Mock<IStatRepository>(); Player player = new Player { Name = "Player1" }; mockClubs.Setup(p => p.Clubs).Returns(new Club[] { new Club { ClubId = 1, Name = "Club1" } }.AsQueryable()); mockPositions.Setup(p => p.Positions).Returns(new Position[] { new Position { PositionId = 1, Name = "Вратарь" } }.AsQueryable()); AdminController targ = new AdminController( mockPlayer.Object, mockClubs.Object, mockPositions.Object, mockStats.Object, null); FormCollection form = new FormCollection { {"PlayerId", "1"}, {"Name", "Player1"}, {"ClubsList", "1"}, {"PositionsList", "1"}, {"Price", "5"}, { "StatId1", "1" }, { "Score", "0" }, { "Concede", "0" }, { "PenaltyScore", "0" }, { "PenaltyMiss", "0" }, { "PenaltySave", "0" }, { "OwnGoal", "0" }, { "YellowCard", "0" }, { "RedCard", "0" }, { "Minutes", "0" }, { "Points", "0" }, }; ActionResult result = targ.Edit(player, form); //mockPlayer.Verify(m => m.SavePlayer(It.IsAny<Player>()), Times.Never()); Assert.IsNotInstanceOfType(result, typeof(ViewResult)); }
public ActionResult Edit(Player player, FormCollection form) { if (ModelState.IsValid) { List<Stat> stats= new List<Stat>(); Player newPlayer = new Player { PlayerId = Convert.ToInt32(form["PlayerId"]), Name = form["Name"], ClubId = Convert.ToInt32(form["ClubsList"]), PositionId = Convert.ToInt32(form["PositionsList"]), Price = Convert.ToInt32(form["Price"]) }; int newPlayerId = repositoryPlayers.SavePlayer(newPlayer); int week = 1; while (form["StatId" + week] != null) { try { stats.Add(new Stat { StatId = Convert.ToInt32(form["StatId" + week]), PlayerId = newPlayerId, Week = week, Score = Convert.ToInt32(form["Score" + week]), Concede = Convert.ToInt32(form["Concede" + week]), PenaltyScore = Convert.ToInt32(form["PenaltyScore" + week]), PenaltyMiss = Convert.ToInt32(form["PenaltyMiss" + week]), PenaltySave = Convert.ToInt32(form["PenaltySave" + week]), OwnGoal = Convert.ToInt32(form["OwnGoal" + week]), YellowCard = Convert.ToInt32(form["YellowCard" + week]), RedCard = Convert.ToInt32(form["RedCard" + week]), Minutes = Convert.ToInt32(form["Minutes" + week]), Points = Convert.ToInt32(form["Points" + week++]) } ); } catch { ModelState.AddModelError("", "Ошибка при вводе статистических данных игрока"); int playerId = player.PlayerId; Player vplayer = repositoryPlayers.Players.FirstOrDefault(p => p.PlayerId == playerId); ViewData["Clubs"] = new SelectList( repositoryClubs.Clubs.OrderBy(c => c.ClubId).ToArray(), "ClubId", "Name", repositoryPlayers.Players.FirstOrDefault(p => p.PlayerId == playerId).ClubId); ViewData["Positions"] = new SelectList( repositoryPositions.Positions.OrderBy(c => c.PositionId).ToArray(), "PositionId", "Name", repositoryPlayers.Players.FirstOrDefault(p => p.PlayerId == playerId).PositionId); return View(vplayer); } } repositoryStats.SaveStat(stats); TempData["message"] = string.Format("{0} данные записаны", player.Name); return RedirectToAction("Index"); } else { int playerId = player.PlayerId; Player vplayer = repositoryPlayers.Players.FirstOrDefault(p => p.PlayerId == playerId); ViewData["Clubs"] = new SelectList( repositoryClubs.Clubs.OrderBy(c => c.ClubId).ToArray(), "ClubId", "Name", repositoryPlayers.Players.FirstOrDefault(p => p.PlayerId == playerId).ClubId); ViewData["Positions"] = new SelectList( repositoryPositions.Positions.OrderBy(c => c.PositionId).ToArray(), "PositionId", "Name", repositoryPlayers.Players.FirstOrDefault(p => p.PlayerId == playerId).PositionId); return View(vplayer); } }
public ViewResult Create() { ViewData["Clubs"] = new SelectList( repositoryClubs.Clubs.OrderBy(c => c.ClubId).ToArray(), "ClubId", "Name", repositoryClubs.Clubs.FirstOrDefault().ClubId); ViewData["Positions"] = new SelectList( repositoryPositions.Positions.OrderBy(c => c.PositionId).ToArray(), "PositionId", "Name", repositoryPositions.Positions.FirstOrDefault().PositionId); Player player = new Player(); player.Stats = new List<Stat>(); byte week = repositoryAppData.AppData.FirstOrDefault().Week; for (int a = 0; a < week; a++) { player.Stats.Add(new Stat { Week = a + 1 }); } return View("Edit", player); }
public int SavePlayer(Player player) { context.Entry(player).State = player.PlayerId == 0 ? EntityState.Added : EntityState.Modified; context.SaveChanges(); return player.PlayerId; }
public void DeletePlayer(Player player) { context.Players.Remove(player); context.SaveChanges(); }
public void Can_Delete_Valid_Player() { Player player = new Player { PlayerId = 2, Name = "Pl2" }; Mock<IPlayerRepository> mock = new Mock<IPlayerRepository>(); mock.Setup(p => p.Players).Returns(new Player[] { new Player { PlayerId = 1, Name = "Player1"}, player, new Player { PlayerId = 3, Name = "Player3"} }.AsQueryable()); AdminController targ = new AdminController(mock.Object, null, null, null, null); targ.Delete(player.PlayerId); mock.Verify(p => p.DeletePlayer(player)); }