public void TestInitialize() { //mock data object to hold fake data// mock = new Mock <IRentalsMock>(); //populate mock list rentals = new List <Rental> { new Rental { RentalId = 777, Title = "Title 1", Address = "107, Georgian Drive", City = "Barrie", State = "ON", Rent = 700, Contact = "111-222-3333", User = new User { UserId = 111, Username = "******", DisplayName = "Colt Stele" } }, new Rental { RentalId = 888, Title = "Title 2", Address = "108, Georgian Drive", City = "Barrie", State = "ON", Rent = 700, Contact = "555-666-7777", User = new User { UserId = 222, Username = "******", DisplayName = "Stephen Grider" } } }; mock.Setup(r => r.Rentals).Returns(rentals.AsQueryable()); controller = new RentalsController(mock.Object); }
protected void BindRentalsList() { //standard lookup try { RentalsController sysmgr = new RentalsController(); List <Rentals> info = null; AddressesController addControl = new AddressesController(); if (!string.IsNullOrEmpty(LandlordList.SelectedValue)) { info = sysmgr.Rentals_FindByLandlord(int.Parse(LandlordList.SelectedValue)); foreach (Rentals item in info) // Loop through List with foreach { item.AddressName = (addControl.Addresses_FindByID(item.AddressID)).FullAddress; } info.Sort((x, y) => x.AddressName.CompareTo(y.AddressName)); AddressSearchList.DataSource = info; AddressSearchList.DataTextField = nameof(Rentals.AddressName); AddressSearchList.DataValueField = nameof(Rentals.RentalID); AddressSearchList.DataBind(); AddressSearchList.Items.Insert(0, "Select an Address..."); } } catch (Exception ex) { errormsgs.Add(GetInnerException(ex).ToString()); LoadMessageDisplay(errormsgs, "alert alert-danger"); } }
public RentalsControllerTests() { _serviceMock = new Mock <IRentalService>(); var config = new MapperConfiguration(opts => opts.AddProfile(typeof(MapperProfile))); var mapper = config.CreateMapper(); _sut = new RentalsController(_serviceMock.Object, mapper); }
public void IndexLoadsView() { //arrange// RentalsController controller = new RentalsController(); //act// ViewResult result = controller.Index() as ViewResult; //assert// Assert.AreEqual("Index", result.ViewName); }
public void Return() { // Arrange RentalsController controller = new RentalsController(); // Act ViewResult result = controller.Return() as ViewResult; // Assert Assert.IsNotNull(result); }
public async Task Post_Create_ModelValid_Redirected() { Mock <IRentalsService> rentalsRepository = MockRentalsService(); Mock <IBooksService> booksRepository = MockBooksService(); Mock <ICustomersService> customersRepository = MockCustomersService(); var controller = new RentalsController(rentalsRepository.Object, booksRepository.Object, customersRepository.Object); var rental = new Rental { }; var result = await controller.Create(rental) as RedirectToActionResult; Assert.AreEqual(result.ActionName, "Index"); }
public async Task Post_Edit_WrongId_NotFoundResult() { Mock <IRentalsService> rentalsRepository = MockRentalsService(); Mock <IBooksService> booksRepository = MockBooksService(); Mock <ICustomersService> customersRepository = MockCustomersService(); var controller = new RentalsController(rentalsRepository.Object, booksRepository.Object, customersRepository.Object); int id = 1; var rental = new Rental { Id = 2 }; var result = await controller.Edit(id, rental); Assert.That(result is NotFoundResult); }
public async Task Post_Create_ModelNotValid_ViewResultWithModel() { Mock <IRentalsService> rentalsRepository = MockRentalsService(); Mock <IBooksService> booksRepository = MockBooksService(); Mock <ICustomersService> customersRepository = MockCustomersService(); var controller = new RentalsController(rentalsRepository.Object, booksRepository.Object, customersRepository.Object); var rental = new Rental { }; controller.ModelState.AddModelError("test", "test"); var result = await controller.Create(rental) as ViewResult; Assert.AreEqual(result.Model, rental); }
protected void FindRentalAddresses_Click(object sender, EventArgs e) { if (LandlordList.SelectedIndex == 0) { errormsgs.Add("Select a landlord to obtain rental address/es."); LoadMessageDisplay(errormsgs, "alert alert-danger"); AddressSearchList.DataSource = null; AddressSearchList.DataBind(); } else { try { // Clear_Click(sender, e); RentalsController sysmgr = new RentalsController(); List <Rentals> info = sysmgr.Rentals_FindByLandlord(int.Parse(LandlordList.SelectedValue)); AddressesController addControl = new AddressesController(); foreach (Rentals item in info) // Loop through List with foreach { item.AddressName = (addControl.Addresses_FindByID(item.AddressID)).FullAddress; } if (info.Count == 0) { errormsgs.Add("No data found for the landlord"); LoadMessageDisplay(errormsgs, "alert alert-danger"); AddressSearchList.DataSource = null; AddressSearchList.DataBind(); } else { info.Sort((x, y) => x.AddressName.CompareTo(y.AddressName)); AddressSearchList.DataSource = info; AddressSearchList.DataTextField = nameof(Rentals.AddressName); AddressSearchList.DataValueField = nameof(Rentals.RentalID); AddressSearchList.DataBind(); AddressSearchList.Items.Insert(0, "Select an Address..."); } } catch (Exception ex) { errormsgs.Add(GetInnerException(ex).ToString()); LoadMessageDisplay(errormsgs, "alert alert-danger"); } } }
public async Task BasicRentTest() { IDataAccess da = new DataAccess(new CostCalculator()); var bikesController = new BikesController(da); var customersController = new CustomersController(da); var rentalsController = new RentalsController(da); var result = await bikesController.PostBike(new BikeRental.Model.Bike { Brand = "brand", PurchaseDate = new DateTime(2019, 11, 11), RentalPriceInEuroForEachAdditionalHour = 3, RentalPriceInEuroForFirstHour = 5, BikeCategory = BikeCategory.Mountainbike, }); var bikeId = ((Bike)((CreatedAtActionResult)result.Result).Value).BikeId; var result2 = await customersController.PostCustomer(new Customer { Gender = CustomerGender.Unknown, FirstName = "fname", LastName = "lname", Birthday = new DateTime(1999, 1, 1), Street = "street", ZipCode = "A-1234", Town = "town", }); var customerId = ((Customer)((CreatedAtActionResult)result2.Result).Value).CustomerId; var result3 = await rentalsController.PostRental(new Rental { RenterId = customerId, BikeId = bikeId, }); var rentalId = ((Rental)((CreatedAtActionResult)result3.Result).Value).RentalId; await rentalsController.EndRental(rentalId); await rentalsController.PayRental(rentalId); await customersController.DeleteCustomer(customerId); await bikesController.DeleteBike(bikeId); }
public RentalsControllerTests(WebApplicationFactory <Startup> factory) { _factory = factory; _client = _factory.CreateClient(); var mockDbContextOptions = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase("TestDatabase") .Options; _db = new ApplicationDbContext(mockDbContextOptions); var mockMapperConfiguration = new MapperConfiguration(cfg => { cfg.AddProfile(new AutoMapperProfile()); }); _mapper = mockMapperConfiguration.CreateMapper(); _controller = new RentalsController(_db, _mapper); }
protected void DeleteRental_Click(object sender, EventArgs e) { int rentalid = 0; if (string.IsNullOrEmpty(RentalID.Text)) { errormsgs.Add("Search for a rental to Remove"); } else if (!int.TryParse(RentalID.Text, out rentalid)) { errormsgs.Add("Rental id is invalid"); } else if (rentalid < 1) { errormsgs.Add("Rental id is invalid"); } if (errormsgs.Count > 0) { LoadMessageDisplay(errormsgs, "alert alert-danger"); } else { try { RentalsController sysmgr = new RentalsController(); //issue the BLL call int rowsaffected = sysmgr.Rentals_Delete(rentalid); //give feedback if (rowsaffected > 0) { errormsgs.Add("Rental has been removed."); LoadMessageDisplay(errormsgs, "alert alert-success"); BindRentalsList(); //by default, list will be at index 0 Clear_Click(sender, e); } else { errormsgs.Add("Rental has not been removed. Rental was not found."); LoadMessageDisplay(errormsgs, "alert alert-danger"); BindRentalsList(); Clear_Click(sender, e); } } catch (DbUpdateException ex) { UpdateException updateException = (UpdateException)ex.InnerException; if (updateException.InnerException != null) { errormsgs.Add(updateException.InnerException.Message.ToString()); } else { errormsgs.Add(updateException.Message); } LoadMessageDisplay(errormsgs, "alert alert-danger"); } catch (DbEntityValidationException ex) { foreach (var entityValidationErrors in ex.EntityValidationErrors) { foreach (var validationError in entityValidationErrors.ValidationErrors) { errormsgs.Add(validationError.ErrorMessage); } } LoadMessageDisplay(errormsgs, "alert alert-danger"); } catch (Exception ex) { errormsgs.Add(GetInnerException(ex).ToString()); LoadMessageDisplay(errormsgs, "alert alert-danger"); } } }
protected void UpdateRental_Click(object sender, EventArgs e) { if (Page.IsValid) { if (int.Parse(Vacancies.Text) > int.Parse(MaxVacancy.Text)) { errormsgs.Add("Vacancies should be less than or equal to the maximum vacancy."); } int rentalid = 0; if (string.IsNullOrEmpty(RentalID.Text)) { errormsgs.Add("Search for a rental to update"); } else if (!int.TryParse(RentalID.Text, out rentalid)) { errormsgs.Add("Rental id is invalid"); } else if (rentalid < 1) { errormsgs.Add("Rental id is invalid"); } if (errormsgs.Count > 0) { LoadMessageDisplay(errormsgs, "alert alert-danger"); } else { try { RentalsController sysmgr = new RentalsController(); Rentals item = new Rentals(); item.RentalID = rentalid; item.AddressID = int.Parse(AddressID.Text); if (RentalTypeList.SelectedIndex == 0) { item.RentalTypeID = null; } else { item.RentalTypeID = int.Parse(RentalTypeList.SelectedValue); } item.MonthlyRent = decimal.Parse(MonthlyRent.Text); item.Vacancies = byte.Parse(Vacancies.Text); item.MaxVacancy = byte.Parse(MaxVacancy.Text); if (string.IsNullOrEmpty(DamageDeposit.Text)) { item.DamageDeposit = null; } else { item.DamageDeposit = decimal.Parse(DamageDeposit.Text); } if (string.IsNullOrEmpty(AvailableDate.Text)) { item.AvailableDate = null; } else { item.AvailableDate = DateTime.Parse(AvailableDate.Text); } int rowsaffected = sysmgr.Rentals_Update(item); if (rowsaffected > 0) { errormsgs.Add("Rental has been updated"); LoadMessageDisplay(errormsgs, "alert alert-success"); BindRentalsList(); AddressSearchList.SelectedValue = RentalID.Text; } else { errormsgs.Add("Rental has not been updated. Rental was not found."); LoadMessageDisplay(errormsgs, "alert alert-danger"); BindRentalsList(); } } catch (DbUpdateException ex) { UpdateException updateException = (UpdateException)ex.InnerException; if (updateException.InnerException != null) { errormsgs.Add(updateException.InnerException.Message.ToString()); } else { errormsgs.Add(updateException.Message); } LoadMessageDisplay(errormsgs, "alert alert-danger"); } catch (DbEntityValidationException ex) { foreach (var entityValidationErrors in ex.EntityValidationErrors) { foreach (var validationError in entityValidationErrors.ValidationErrors) { errormsgs.Add(validationError.ErrorMessage); } } LoadMessageDisplay(errormsgs, "alert alert-danger"); } catch (Exception ex) { errormsgs.Add(GetInnerException(ex).ToString()); LoadMessageDisplay(errormsgs, "alert alert-danger"); } } } }
protected void FindRental_Click(object sender, EventArgs e) { if (AddressSearchList.SelectedIndex == 0 || AddressSearchList.Items.Count < 1) { errormsgs.Add("Select a rental address to maintain."); LoadMessageDisplay(errormsgs, "alert alert-danger"); } else { try { RentalsController rentalsctrlr = new RentalsController(); Rentals info = null; info = rentalsctrlr.Rentals_FindByID(int.Parse(AddressSearchList.SelectedValue)); AddressesController addressctrlr = new AddressesController(); Addresses address = null; List <Addresses> alladdresses = addressctrlr.Address_List(); address = addressctrlr.Addresses_FindByID(info.AddressID); if (info == null) { errormsgs.Add("Cannot find Rental in database."); LoadMessageDisplay(errormsgs, "alert alert-danger"); Clear_Click(sender, e); } else { RentalID.Text = info.RentalID.ToString(); AddressNumber.Text = address.Number; AddressStreet.Text = address.Street; alladdresses.Sort((x, y) => x.FullAddress.CompareTo(y.FullAddress)); AddressDetailList.DataSource = alladdresses; AddressDetailList.DataTextField = nameof(Addresses.FullAddress); AddressDetailList.DataValueField = nameof(Addresses.AddressID); AddressDetailList.DataBind(); AddressDetailList.Items.Insert(0, "Select an Address..."); AddressDetailList.SelectedValue = address.AddressID.ToString(); AddressID.Text = address.AddressID.ToString(); SelectedAddress.Text = address.FullAddress; if (info.RentalTypeID.HasValue) { RentalTypeList.SelectedValue = info.RentalTypeID.ToString(); } else { RentalTypeList.SelectedIndex = 0; } MonthlyRent.Text = string.Format("{0:0.00}", info.MonthlyRent); Vacancies.Text = info.Vacancies.ToString(); MaxVacancy.Text = info.MaxVacancy.ToString(); DamageDeposit.Text = string.IsNullOrEmpty(info.DamageDeposit.ToString()) ? "" : string.Format("{0:0.00}", info.DamageDeposit); AvailableDate.Text = string.IsNullOrEmpty(info.AvailableDate.ToString()) ? "" : info.AvailableDate.Value.ToString("yyyy-MM-dd"); } } catch (Exception ex) { errormsgs.Add(GetInnerException(ex).ToString()); LoadMessageDisplay(errormsgs, "alert alert-danger"); } } }
public RentalsControllerShould() { _controller = new RentalsController(MockRentalService.Object); }