예제 #1
0
        public void Calculate_Cart_Total()
        {
            // arrange
            Game game1 = new Game { GameId = 1, Name = "Sport1" };
            Game game2 = new Game { GameId = 2, Name = "Sport2" };
            Game game3 = new Game { GameId = 3, Name = "Sport3" };

            Cart cart = new Cart();
            //Act
            cart.AddItem(game1, 1);
            cart.AddItem(game2, 1);
            cart.AddItem(game1, 5);
            cart.Clear();

            // Assert
            Assert.AreEqual(cart.Lines.Count(), 0);
        }
예제 #2
0
 public void Can_Delete_Valid_Games()
 {
     Game game = new Game { GameId = 2, Name = "Game2" };
     // Arrange
     Mock<IGameRepository> mock = new Mock<IGameRepository>();
     mock.Setup(m => m.Games).Returns(new List<Game>
     {
         new Game { GameId = 1, Name = "Game1"},
         new Game { GameId = 2, Name = "Game2"},
         new Game { GameId = 3, Name = "Game3"},
         new Game { GameId = 4, Name = "Game4"},
         new Game { GameId = 5, Name = "Game5"}
     });
     AdminController ctrl = new AdminController(mock.Object);
     //Act
     ctrl.Delete(game.GameId);
     mock.Verify(m => m.DeleteGame(game.GameId));
 }
예제 #3
0
        public ActionResult Edit(Game game, HttpPostedFileBase image = null)
        {
            if (ModelState.IsValid)
            {
                game.ImageMimeType = image.ContentType;
                game.ImagePath = Path.GetFileName(Guid.NewGuid().ToString() + "_" +
                                       Path.GetFileName(image.FileName));
                image.SaveAs(Server.MapPath("~/Image/" + game.ImagePath));
                repository.SaveGame(game);
                TempData["message"] = string.Format("Изменения в игре \"{0}\" были сохранены", game.Name);

                return RedirectToAction("Index");
            }
            else
            {
                return View(game);
            }
        }
예제 #4
0
        public void Cannot_Save_Invalid_Changes()
        {
            // Arrange
              Mock<IGameRepository> mock = new Mock<IGameRepository>();

            AdminController controller = new AdminController(mock.Object);

            Game game = new Game { Name = "Test" };

            controller.ModelState.AddModelError("error", "error");

            // Act
            ActionResult result = controller.Edit(game);

            // Assert appeal to the repository does not produce
            mock.Verify(m => m.SaveGame(It.IsAny<Game>()), Times.Never());

            Assert.IsInstanceOfType(result, typeof(ViewResult));
        }
예제 #5
0
        public void AddItem(Game game, int quantity)
        {
            CartLine line = lineCollection
                .Where(g => g.Game.GameId == game.GameId)
                .FirstOrDefault();

            if (line == null)
            {
                lineCollection.Add(new CartLine
                {
                    Game = game,
                    Quantity = quantity
                });
            }
            else
            {
                line.Quantity += quantity;
            }
        }
예제 #6
0
 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.ImagePath = game.ImagePath;
             dbEntry.ImageMimeType = game.ImageMimeType;
         }
     }
     context.SaveChanges();
 }
예제 #7
0
        public void Can_Save_Valid_Changes()
        {
            // Arrange
            Mock<IGameRepository> mock = new Mock<IGameRepository>();
            AdminController controller = new AdminController(mock.Object);
            Game game = new Game { Name = "Test" };

            // Act
            ActionResult result = controller.Edit(game);
            mock.Verify(m => m.SaveGame(game));
            // Assert
            Assert.IsNotInstanceOfType(result, typeof(ViewResult));
        }
예제 #8
0
 public void RemoveLine(Game game)
 {
     lineCollection.RemoveAll(l => l.Game.GameId == game.GameId);
 }
예제 #9
0
        public void Can_Add_New_Lines()
        {
            // arrange
            Game game1 = new Game { GameId = 1, Name = "Sport1" };
            Game game2 = new Game { GameId = 2, Name = "Sport2" };

            Cart cart = new Cart();

            // Act
            cart.AddItem(game1, 1);
            cart.AddItem(game2, 1);
            List<CartLine> results = cart.Lines.ToList();

            // Assert
            Assert.AreEqual(results.Count(), 2);
            Assert.AreEqual(results[0].Game, game1);
            Assert.AreEqual(results[1].Game, game2);
        }
예제 #10
0
        public void Can_Remove_Line()
        {
            // arrange
            Game game1 = new Game { GameId = 1, Name = "Sport1" };
            Game game2 = new Game { GameId = 2, Name = "Sport2" };
            Game game3 = new Game { GameId = 3, Name = "Sport3" };

            Cart cart = new Cart();
            //Act

            cart.AddItem(game1, 1);
            cart.AddItem(game2, 4);
            cart.AddItem(game3, 2);
            cart.AddItem(game2, 1);

            //Act
            cart.RemoveLine(game2);

            //Assert
            Assert.AreEqual(cart.Lines.Where(c => c.Game == game2).Count(), 0);
            Assert.AreEqual(cart.Lines.Count(), 2);
        }
예제 #11
0
        public void Can_Add_Quantity_For_Existing_Lines()
        {
            // arrange
            Game game1 = new Game { GameId = 1, Name = "Sport1" };
            Game game2 = new Game { GameId = 2, Name = "Sport2" };

            Cart cart = new Cart();
            //Act
            cart.AddItem(game1, 1);
            cart.AddItem(game2, 1);
            cart.AddItem(game1, 5);
            List<CartLine> results = cart.Lines.OrderBy(c => c.Game.GameId).ToList();

            // Assetr
            Assert.AreEqual(results.Count(), 2);
            Assert.AreEqual(results[0].Quantity, 6);    // 6 экземпляров добавлено в корзину
            Assert.AreEqual(results[1].Quantity, 1);
        }