예제 #1
0
        public void TestSearchStocksAsync_OK()
        {
            // Create data from external API
            dynamic externalData = new
            {
                result = new List <dynamic>()
                {
                    new { symbol = "AAA", description = "Company A" }
                }
            };
            // Create HTTP response from external API
            var httpResponse = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(JsonConvert.SerializeObject(externalData), Encoding.UTF8, "application/json")
            };

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

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

            // Verify result
            Assert.AreEqual(externalData.result.Count, result.Count);
            Assert.AreEqual(externalData.result[0].symbol, result[0].Symbol);
            Assert.AreEqual(externalData.result[0].description, result[0].Name);
        }