public void Can_Retrieve_Image_Data() { // Arrange - create a Tour with image data Tour tour = new Tour { TourID = 2, Name = "Test", ImageData = new byte[] { }, ImageMimeType = "image/png" }; // Arrange - create the mock repository Mock<ITourRepository> mock = new Mock<ITourRepository>(); mock.Setup(m => m.Tours).Returns(new Tour[] { new Tour {TourID = 1, Name = "P1"}, tour, new Tour {TourID = 3, Name = "P3"} }.AsQueryable()); // Arrange - create the controller TourController target = new TourController(mock.Object); // Act - call the GetImage action method ActionResult result = target.GetImage(2); // Assert Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(FileResult)); Assert.AreEqual(tour.ImageMimeType, ((FileResult)result).ContentType); }
public void AddItem(Tour tour, int quantity) { CartLine line = lineCollection .Where(t => t.Tour.TourID == tour.TourID) .FirstOrDefault(); if (line == null) { lineCollection.Add(new CartLine { Tour = tour, Quantity = quantity }); } else { line.Quantity += quantity; } }
public void Can_Add_New_Lines() { // Arrange - create some test products Tour p1 = new Tour { TourID = 1, Name = "P1" }; Tour p2 = new Tour { TourID = 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].Tour, p1); Assert.AreEqual(results[1].Tour, p2); }
public void Can_Add_Quantity_For_Existing_Lines() { // Arrange - create some test products Tour p1 = new Tour { TourID = 1, Name = "P1" }; Tour p2 = new Tour { TourID = 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.Tour.TourID).ToArray(); // Assert Assert.AreEqual(results.Length, 2); Assert.AreEqual(results[0].Quantity, 11); Assert.AreEqual(results[1].Quantity, 1); }
public ActionResult Edit(Tour tour, HttpPostedFileBase image = null) { if (ModelState.IsValid) { if (image != null) { tour.ImageMimeType = image.ContentType; tour.ImageData = new byte[image.ContentLength]; image.InputStream.Read(tour.ImageData, 0, image.ContentLength); } repository.SaveTour(tour); TempData["message"] = string.Format("{0} has been saved", tour.Name); return RedirectToAction("Index"); } else { // there is something wrong with the data values return View(tour); } }
public void SaveTour(Tour tour) { if (tour.TourID == 0) { context.Tours.Add(tour); } else { Tour dbEntry = context.Tours.Find(tour.TourID); if (dbEntry != null) { dbEntry.Name = tour.Name; dbEntry.Description = tour.Description; dbEntry.Price = tour.Price; dbEntry.Category = tour.Category; dbEntry.ImageData = tour.ImageData; dbEntry.ImageMimeType = tour.ImageMimeType; } } context.SaveChanges(); }
public void Can_Remove_Line() { // Arrange - create some test products Tour p1 = new Tour { TourID = 1, Name = "P1" }; Tour p2 = new Tour { TourID = 2, Name = "P2" }; Tour p3 = new Tour { TourID = 3, Name = "P3" }; // Arrange - create a new cart Cart target = new Cart(); // Arrange - add some products 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.Tour == p2).Count(), 0); Assert.AreEqual(target.Lines.Count(), 2); }
public void RemoveLine(Tour tour) { lineCollection.RemoveAll(l => l.Tour.TourID == tour.TourID); }
public void Can_Save_Valid_Changes() { // Arrange - create mock repository Mock<ITourRepository> mock = new Mock<ITourRepository>(); // Arrange - create the controller AdminController target = new AdminController(mock.Object); // Arrange - create a tour Tour tour = new Tour { Name = "Test" }; // Act - try to save the tour ActionResult result = target.Edit(tour); // Assert - check that the repository was called mock.Verify(m => m.SaveTour(tour)); // Assert - check the method result type Assert.IsNotInstanceOfType(result, typeof(ViewResult)); }
public void Can_Delete_Valid_Tours() { // Arrange - create a Tour Tour tour = new Tour { TourID = 2, Name = "Test" }; // Arrange - create the mock repository Mock<ITourRepository> mock = new Mock<ITourRepository>(); mock.Setup(m => m.Tours).Returns(new Tour[] { new Tour {TourID = 1, Name = "P1"}, tour, new Tour {TourID = 3, Name = "P3"}, }); // Arrange - create the controller AdminController target = new AdminController(mock.Object); // Act - delete the touruct target.Delete(tour.TourID); // Assert - ensure that the repository delete method was // called with the correct Tour mock.Verify(m => m.DeleteTour(tour.TourID)); }
public void Cannot_Save_Invalid_Changes() { // Arrange - create mock repository Mock<ITourRepository> mock = new Mock<ITourRepository>(); // Arrange - create the controller AdminController target = new AdminController(mock.Object); // Arrange - create a tour Tour tour = new Tour { Name = "Test" }; // Arrange - add an error to the model state target.ModelState.AddModelError("error", "error"); // Act - try to save the tour ActionResult result = target.Edit(tour); // Assert - check that the repository was not called mock.Verify(m => m.SaveTour(It.IsAny<Tour>()), Times.Never()); // Assert - check the method result type Assert.IsInstanceOfType(result, typeof(ViewResult)); }
public void Calculate_Cart_Total() { // Arrange - create some test products Tour p1 = new Tour { TourID = 1, Name = "P1", Price = 100M }; Tour p2 = new Tour { TourID = 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 void Can_Clear_Contents() { // Arrange - create some test products Tour p1 = new Tour { TourID = 1, Name = "P1", Price = 100M }; Tour p2 = new Tour { TourID = 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, 1); // Act - reset the cart target.Clear(); // Assert Assert.AreEqual(target.Lines.Count(), 0); }