Ejemplo n.º 1
0
        public void TestGetStockAsync_OK()
        {
            // Create data from external API
            string  quoteString       = "123.45";
            dynamic externalStockData = new
            {
                name     = "Some Company",
                currency = "USD"
            };
            // Create quote HTTP response from external API
            var quoteHttpResponse = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(quoteString, 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(quoteHttpResponse)
            .ReturnsAsync(stockDataHttpResponse);
            _httpClientFactory.Setup(a => a.CreateClient(It.IsAny <string>())).Returns(_httpClient.Object);

            // Run test
            var symbol = "abc";
            var result = _stocksService.GetStockAsync(symbol).Result;

            // Verify result
            Assert.AreEqual(quoteString, result.Quote.Price.ToString());
            Assert.AreEqual(externalStockData.currency, result.Quote.Currency.ToString());
            Assert.AreEqual(externalStockData.name, result.Name);
            Assert.AreEqual(symbol, result.Symbol);
        }