public ActionResult Edit(Game game) { if (ModelState.IsValid) { repository.SaveGame(game); TempData["message"] = string.Format("{0} has been saved", game.Name); return RedirectToAction("Index"); } else { // There is something wrong with the data values return View(game); } }
public void Calculate_Cart_Total() { // Arrange - Create some test games Game p1 = new Game { GameID = 1, Name = "P1", Price = 100M }; Game p2 = new Game { GameID = 2, Name = "P2", Price = 50M }; // Arrange - Create a new cart Cart target = new Cart(); // Act target.AddItem(p1, 1); target.AddItem(p2, 1); target.AddItem(p1, 3); decimal result = target.ComputeTotalValue(); // Assert Assert.AreEqual(result, 450M); }
public ActionResult Edit(Game game, HttpPostedFileBase image) { if (ModelState.IsValid) { if (image != null) { game.ImageMimeType = image.ContentType; game.ImageData = new byte[image.ContentLength]; image.InputStream.Read(game.ImageData, 0, image.ContentLength); } repository.SaveGame(game); TempData["message"] = string.Format("{0} has been saved", game.Name); return RedirectToAction("Index"); } else { // There is something wrong with the data values return View(game); } }
public void SaveGame(Game game) { if (game.GameID == 0) { context.Games.Add(game); } else { Game dbEntry = context.Games.Find(game.GameID); if (dbEntry != null) { dbEntry.Name = game.Name; dbEntry.Description = game.Description; dbEntry.Price = game.Price; dbEntry.Category = game.Category; dbEntry.ImageData = game.ImageData; dbEntry.ImageMimeType = game.ImageMimeType; } } context.SaveChanges(); }
public void Cannot_Save_Invalid_Changes() { // Arrange - Create mock repository Mock<IGameRepository> mock = new Mock<IGameRepository>(); // Arrange - Create the controller AdminController target = new AdminController(mock.Object); // Arrange - Create a game Game game = new Game { Name = "Test" }; // Arrange - Add an error to the model state target.ModelState.AddModelError("error", "error"); // Act - Try to save the game ActionResult result = target.Edit(game); // Assert - Check that the repository was not called mock.Verify(m => m.SaveGame(It.IsAny<Game>()), Times.Never()); // Assert - Check the method result type Assert.IsNotInstanceOfType(result, typeof(ViewResult)); }
public void Can_Save_Valid_Changes() { // Arrange - Create mock repository Mock<IGameRepository> mock = new Mock<IGameRepository>(); // Arrange - Create the controller AdminController target = new AdminController(mock.Object); // Arrange - Create a game Game game = new Game { Name = "Test" }; // Act - Try to save the game ActionResult result = target.Edit(game); // Assert - Check that the repository was called mock.Verify(m => m.SaveGame(game)); // Assert - Check the method result type Assert.IsNotInstanceOfType(result, typeof(ViewResult)); }
public void Can_Remove_Line() { // Arrange - Create some test games Game p1 = new Game { GameID = 1, Name = "P1" }; Game p2 = new Game { GameID = 2, Name = "P2" }; Game p3 = new Game { GameID = 3, Name = "P3" }; // Arrange - Create a new cart Cart target = new Cart(); // Arrange - add some games to the cart target.AddItem(p1, 1); target.AddItem(p2, 3); target.AddItem(p3, 5); target.AddItem(p2, 1); // Act target.RemoveLine(p2); // Assert Assert.AreEqual(target.Lines.Where(c => c.Game == p2).Count(), 0); Assert.AreEqual(target.Lines.Count(), 2); }
public void Can_Clear_Contents() { // Arrange - Create some test games Game p1 = new Game { GameID = 1, Name = "P1", Price = 100M }; Game p2 = new Game { GameID = 2, Name = "P2", Price = 50M }; // Arrange - Create a new cart Cart target = new Cart(); // Arrange - Add some items target.AddItem(p1, 1); target.AddItem(p2, 2); // Act - Reset the cart target.Clear(); // Assert Assert.AreEqual(target.Lines.Count(), 0); }
public void Can_Add_Quality_For_Existing_Lines() { //Arrange - Create some test games Game p1 = new Game { GameID = 1, Name = "P1" }; Game p2 = new Game { GameID = 2, Name = "P2" }; // Arrange - Create a new cart Cart target = new Cart(); // Act target.AddItem(p1, 1); target.AddItem(p2, 1); target.AddItem(p1, 10); CartLine[] results = target.Lines.OrderBy(c => c.Game.GameID).ToArray(); // Assert Assert.AreEqual(results.Length, 2); Assert.AreEqual(results[0].Quantity, p1); Assert.AreEqual(results[1].Quantity, p2); }
public void Can_Add_New_Lines() { // Arrange - Create some test games Game p1 = new Game { GameID = 1, Name = "P1" }; Game p2 = new Game { GameID = 2, Name = "P2" }; // Arrange - Create a new cart Cart target = new Cart(); // Act target.AddItem(p1, 1); target.AddItem(p2, 1); CartLine[] results = target.Lines.ToArray(); // Assert Assert.AreEqual(results.Length, 2); Assert.AreEqual(results[0].Game, p1); Assert.AreEqual(results[1].Game, p2); }