示例#1
0
        public void GetFlightPlan()
        {
            // Arrange
            var mockFactory       = new Mock <IHttpClientFactory>();
            var clientHandlerStub = new DelegatingHandlerStub();
            var client            = new HttpClient(clientHandlerStub);

            mockFactory.Setup(_ => _.CreateClient(It.IsAny <string>())).Returns(client);

            IHttpClientFactory factory     = mockFactory.Object;
            IMemoryCache       memoryCache = new MemoryCache(new MemoryCacheOptions());

            Mock <IMemoryCache> cache = new Mock <IMemoryCache>();

            var myContext = new Mock <HttpContext>();

            DateTime   now        = DateTime.Now;
            FlightPlan flightPlan = new FlightPlan();

            flightPlan.company_name               = "swiss";
            flightPlan.passengers                 = 200;
            flightPlan.initial_location           = new InitialLocation();
            flightPlan.initial_location.date_time = DateTime.Now;
            flightPlan.segments = new List <Segments>();
            flightPlan.segments.Add(new Segments());
            flightPlan.segments[0].timespan_seconds = 100;

            var          json      = JsonConvert.SerializeObject(flightPlan);
            var          byteArray = Encoding.ASCII.GetBytes(json);
            MemoryStream stream    = new MemoryStream(byteArray);

            stream.Flush();
            stream.Position = 0;

            myContext.SetupGet(x => x.Request.Body).Returns(stream);
            var controllerContext = new ControllerContext()
            {
                HttpContext = myContext.Object,
            };
            FlightPlanController controller = new FlightPlanController(memoryCache, factory)
            {
                ControllerContext = controllerContext,
            };

            // Act
            controller.AddFlightPlan(flightPlan);

            // Arrange
            myContext = new Mock <HttpContext>();
            myContext.SetupGet(x => x.Request.QueryString).
            Returns(new QueryString("?relative_to=" + DateTime.Now));
            controllerContext = new ControllerContext()
            {
                HttpContext = myContext.Object,
            };
            controller = new FlightPlanController(memoryCache, factory)
            {
                ControllerContext = controllerContext,
            };

            //Act
            var list = controller.GetFlightsByDateAsync(DateTime.Now);

            // Assert
            Assert.IsTrue(list.Result.Value.Count == 1);

            // Act
            Task <ActionResult <FlightPlan> > answer =
                controller.GetFlightPlanById(list.Result.Value[0].flight_id);

            // Assert
            Assert.AreEqual(answer.Result.Value, flightPlan);

            // Act
            controller.DeleteFlight(list.Result.Value[0].flight_id);
            list = controller.GetFlightsByDateAsync(DateTime.Now);

            // Assert
            Assert.IsTrue(list.Result.Value.Count == 0);
        }