public void TestReservedParkingSpaces() { var car = GenerateCar(501, 2, 4, true); var cars = GenerateCars(4, 2, false, 450); var mock = new Mock<ICarsParkingRepository>(); mock.Setup(m => m.Cars).Returns(cars.AsQueryable()); var target = new ParkingController(mock.Object, Repository); target.AddParkingCar(car); }
public void TestLimitCap() { var car = GenerateCar(501, 4, 2, true); var cars = GenerateCars(4, 2, true, 500); var mock = new Mock<ICarsParkingRepository>(); var enumerable = cars as IList<Car> ?? cars.ToList(); mock.Setup(m => m.Cars).Returns(enumerable.AsQueryable()); var target = new ParkingController(mock.Object, Repository); var cannotAddCars = target.AddParkingCar(car); Assert.IsNotNull(cannotAddCars); Assert.AreEqual(500, enumerable.Count()); var result = target.List(); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(ViewResult)); }
/// <summary> /// Adds cars into parking spaces according if car has contract or not. /// </summary> /// <param name="car">Car which is being added</param> /// <param name="parkingController">Used to set modelstate to false if any errors occur</param> public void AddCarToParkingLot(Car car, ParkingController parkingController) { var carsInParkingLotWithContract = context.Cars.Count(c => c.HasContract); var carsInParkingLot = context.Cars.Count(); if (!HasFreeParkingSpaces(carsInParkingLot)) { parkingController.ModelState.AddModelError("error", "The parking house is full. Come back later."); return; } // try to add a car with contract if (CanAddCarsWhenNoFreeSpaceLeft(carsInParkingLot, carsInParkingLotWithContract, car.HasContract)) { context.Cars.Add(car); return; } // try to add a car with no contract if (CanAddCarsWhenNoFreeSpaceLeft(carsInParkingLot, carsInParkingLotWithContract, !car.HasContract)) { var reservedSpaces = GetReservedSpaces(carsInParkingLotWithContract); if (GetFreeSpaces(carsInParkingLotWithContract, reservedSpaces) <= 0) { parkingController.ModelState.AddModelError("error", string.Format( "Last {0} spot(s) reserved for clients with a contract. Come back later.", reservedSpaces)); return; } // Add a car only if there are free spaces left context.Cars.Add(car); } }