示例#1
0
        public void TestGetStockHistoryAsync_OK()
        {
            // Create data from external API
            var externalStockHistory = new dynamic[]
            {
                new
                {
                    date  = "2021-03-01",
                    close = "123.45"
                },
                new
                {
                    date  = "2021-03-02",
                    close = "234.56"
                }
            };
            dynamic externalStockData = new
            {
                name = "Some Company"
            };

            // Create quote HTTP response from external API
            var stockHistoryHttpResponse = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(JsonConvert.SerializeObject(externalStockHistory), Encoding.UTF8, "application/html")
            };
            // Create stock data HTTP response from external API
            var stockDataHttpResponse = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(JsonConvert.SerializeObject(externalStockData), Encoding.UTF8, "application/json")
            };

            // Set up HTTP client result
            _httpMessageHandler.Protected().SetupSequence <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(stockHistoryHttpResponse)
            .ReturnsAsync(stockDataHttpResponse);
            _httpClientFactory.Setup(a => a.CreateClient(It.IsAny <string>())).Returns(_httpClient.Object);

            // Run test
            var symbol = "abc";
            var result = _stocksService.GetStockHistoryAsync(symbol, DateRange.FIVEDAY).Result;

            // Verify result
            var stockResult = result.Stock;

            Assert.AreEqual(externalStockData.name, stockResult.Name);
            Assert.AreEqual(symbol, stockResult.Symbol);
            var historyResult = result.PriceHistory;

            Assert.AreEqual(externalStockHistory.Length, historyResult.Count);
            Assert.AreEqual(externalStockHistory[0].date, historyResult[0].Date.ToString("yyyy-MM-dd"));
            Assert.AreEqual(externalStockHistory[0].close, historyResult[0].Price.ToString());
            Assert.AreEqual(StockCurrency.USD, historyResult[0].Currency);
        }