/// This test expects Create to show View public void Create_Should_Show_View() { //Arrange //Act var result = sut.Create() as ViewResult; // Assert Assert.IsNotNull(result); }
public void Create_ShouldAddFlight() { // Arrange var flightToAdd = new Flight( new Airport("HND", "Tokyo Haneda International Airport (Japan)", 35.552299, 139.779999), new Airport("LAX", "Los Angeles International Airport (United States)", 33.94250107, -118.4079971), new Aircraft("OO-JAA", "VICTORY", 0, 0) ); FlightsController controller = new FlightsController(this.flightRepository); // Act var actionResult = controller.Create(flightToAdd); // Asserts // Assert 1 : Call OK - 200 Assert.IsType <CreatedAtActionResult>(actionResult.Result); // retrieve the new flight in the repository var newFlightIdFromApi = (Guid)(actionResult.Result as CreatedAtActionResult).RouteValues["id"]; var flightFromRepository = this.flightRepository.GetById(newFlightIdFromApi); // Assert 2 : check created flight match the new one Assert.Equal(flightToAdd.Aircraft.Name, flightFromRepository.Aircraft.Name); Assert.Equal(flightToAdd.DepartureAirport.Name, flightFromRepository.DepartureAirport.Name); Assert.Equal(flightToAdd.DestinationAirport.Name, flightFromRepository.DestinationAirport.Name); }
public void Create_many_flights(int id) { var options = new DbContextOptionsBuilder <FlyingContext>() .UseInMemoryDatabase(databaseName: "create_many_database") .Options; // Run the test against one instance of the context using (var context = new FlyingContext(options)) { // Create a new flight var flight = new Flight(); flight.FlightId = id; flight.DepartureName = "Ronald Reagan Washington National Airport" + id; flight.DepartureLatitude = 38.8521d; flight.DepartureLongitude = -77.037697d; flight.DestinationName = "Ken Jones Airport" + id; flight.DestinationLatitude = 18.1987991333d; flight.DestinationLongitude = -76.53450012210001d; flight.FuelConsumption = 1.26d; flight.TakeoffEffort = 1.001d; flight.Speed = 500; // Save the flight in the data base var controller = new FlightsController(context); var result = controller.Create(flight).Result as RedirectToActionResult; Assert.IsNotNull(result, $"Flight {id} was not created"); Assert.AreEqual("Index", result.ActionName); } }
public async Task CreateGet_ValidRequest_Success() { // Arrange var expectedAirports = _fixture.CreateMany <Airport>().ToList(); _mediatorMock .Setup(x => x.Send( It.IsAny <AirportAll.Query>(), It.IsAny <CancellationToken>())) .ReturnsAsync(expectedAirports); var target = new FlightsController(_mediatorMock.Object); // Act var result = await target.Create(); // Assert var viewResult = result .Should() .BeOfType <ViewResult>().Subject; var model = viewResult.ViewData.Model .Should() .BeOfType <AddFlightViewModel>().Subject; model.Airports .Should() .HaveCount(expectedAirports.Count); }
public async Task CreatePost_ValidRequest_Success() { // Arrange var viewModel = _fixture.Create <AddFlightViewModel>(); var tempData = new TempDataDictionary( new DefaultHttpContext(), Mock.Of <ITempDataProvider>()) { [nameof(DefaultMessages.FlightCreatedMessage)] = DefaultMessages.FlightCreatedMessage }; var target = new FlightsController(_mediatorMock.Object) { TempData = tempData }; // Act var result = await target.Create(viewModel); // Assert var viewResult = result .Should() .BeOfType <RedirectToActionResult>().Subject; viewResult.ActionName .Should() .Be(nameof(FlightsController.Index)); _mediatorMock.Verify(x => x.Send( It.IsAny <FlightCreate.Command>(), It.IsAny <CancellationToken>())); }
public void FlightsCreateLoad() { //Act var result = controller.Create() as ViewResult; //Assert Assert.AreEqual("Create", result.ViewName); }
public async Task CreateGet_ViewBagShouldContainAirportsAndAirplanes() { // Arrange IEnumerable <Airport> airports = new List <Airport>(); IEnumerable <Airplane> airplanes = new List <Airplane>(); _unitOfWorkMock.Setup(uof => uof.AirplaneRepository.GetAllAsync()).Returns(Task.FromResult(airplanes)); _unitOfWorkMock.Setup(uof => uof.AirportRepository.GetAllAsync()).Returns(Task.FromResult(airports)); //Act var result = await _flightsController.Create(); //Assert var viewResult = Assert.IsType <ViewResult>(result); Assert.IsType <List <Airport> >(_flightsController.ViewBag.Airports); Assert.IsType <List <Airplane> >(_flightsController.ViewBag.Airplanes); _unitOfWorkMock.Verify(uof => uof.AirplaneRepository.GetAllAsync()); _unitOfWorkMock.Verify(uof => uof.AirportRepository.GetAllAsync()); }
static void Main(string[] args) { var controllerf = new FlightsController(); var flight = new FlightPostModel() { FlightCity = "Kiev - New-York", FlightTime = DateTime.Now, Pilot = new PilotPostModel() { FirstName = "Igor", LastName = "Ivanov", }, Plane = new PlanePostModel() { ModelOfPlane = "Boeing 737", PassengerCapacity = 180 } }; var flightcreate = controllerf.Create(flight); }
public async Task CreatePost_SameAirports_ReturnsError() { // Arrange var airportId = Guid.NewGuid(); var viewModel = new AddFlightViewModel { DepartureAirportId = airportId, DestinationAirportId = airportId }; var expectedAirports = _fixture.CreateMany <Airport>().ToList(); _mediatorMock .Setup(x => x.Send( It.IsAny <AirportAll.Query>(), It.IsAny <CancellationToken>())) .ReturnsAsync(expectedAirports); var target = new FlightsController(_mediatorMock.Object); // Act var result = await target.Create(viewModel); // Assert var viewResult = result .Should() .BeOfType <ViewResult>().Subject; var model = viewResult.ViewData.Model .Should() .BeOfType <AddFlightViewModel>().Subject; model.Airports .Should() .HaveCount(expectedAirports.Count); target.ModelState.IsValid.Should().BeFalse(); }
static void Main(string[] args) { var controllerf = new FlightsController(); var flight = new FlightPostModel() { FlightCity = "Kiev - New-York", FlightTime = DateTime.Now, //Pilots = new List<PilotPostModel>() //{ // new PilotPostModel // { // FirstName = "Aslan", // LastName = "Saber" // } //}, Plane = new PlanePostModel() //{ // ModelOfPlane = "Boeing 737", // PassengerCapacity = 180 //} }; var flightcreate = controllerf.Create(flight); }
public void Create_modify_verify_one_flight() { var options = new DbContextOptionsBuilder <FlyingContext>() .UseInMemoryDatabase(databaseName: "create_one_database") .Options; // Run the test against one instance of the context using (var context = new FlyingContext(options)) { var controller = new FlightsController(context); // Create a new flight var flight = new Flight(); flight.FlightId = 999; flight.DepartureName = "Ronald Reagan Washington National Airport"; flight.DepartureLatitude = 38.8521d; flight.DepartureLongitude = -77.037697d; flight.DestinationName = "Ken Jones Airport"; flight.DestinationLatitude = 18.1987991333d; flight.DestinationLongitude = -76.53450012210001d; flight.FuelConsumption = 1.26d; flight.TakeoffEffort = 1.001d; flight.Speed = 500; var result = controller.Create(flight).Result as RedirectToActionResult; // Check the calculations Assert.AreEqual(flight.FuelAmount, 2897); Assert.AreEqual(flight.FlightTime, TimeSpan.FromMinutes(4 * 60 + 36)); Assert.AreEqual(flight.Distance, 2299); Assert.AreEqual("Index", result.ActionName); } // Run the test against one instance of the context using (var context = new FlyingContext(options)) { // Get the view details var controller = new FlightsController(context); var result = controller.Details(999).Result as ViewResult; var flight = (Flight)result.ViewData.Model; // Check the calculations Assert.AreEqual(flight.FuelAmount, 2897); Assert.AreEqual(flight.FlightTime, TimeSpan.FromMinutes(4 * 60 + 36)); Assert.AreEqual(flight.Distance, 2299); Assert.AreEqual("Details", result.ViewName); } // Run the test against one instance of the context using (var context = new FlyingContext(options)) { // Get edit view var controller = new FlightsController(context); var result = controller.Edit(999).Result as ViewResult; var flight = (Flight)result.ViewData.Model; Assert.AreEqual("Edit", result.ViewName); // Modify input informations flight.DepartureName = "Narita International Airport"; flight.DepartureLatitude = 35.7647018433d; flight.DepartureLongitude = 140.386001587d; flight.DestinationName = "Kabul International Airport"; flight.DestinationLatitude = 34.56589889526367d; flight.DestinationLongitude = 69.2123031616211d; flight.FuelConsumption = 2.79d; flight.TakeoffEffort = 1.004d; flight.Speed = 833; // Save edit view result = controller.Edit(999, flight).Result as ViewResult; } // Run the test against one instance of the context using (var context = new FlyingContext(options)) { // Check the calculations var controller = new FlightsController(context); var result = controller.Details(999).Result as ViewResult; var flight = (Flight)result.ViewData.Model; Assert.AreEqual(flight.FuelAmount, 17644); Assert.AreEqual(flight.FlightTime, TimeSpan.FromMinutes(7 * 60 + 36)); Assert.AreEqual(flight.Distance, 6323); } }