Ejemplo n.º 1
0
        public async Task ShouldReturnPosts()
        {
            var    handlerMock = FakeHttpMessageHandler.NewMock();
            string json        = "";
            string path        = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory.Replace("\\bin\\Debug", "")) + ConfigurationManager.AppSettings["TestJson"];
            var    directory   = System.IO.Path.GetDirectoryName(path);

            using (StreamReader r = new StreamReader(path))
            {
                json = r.ReadToEnd();
            }
            var response = new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent(json),
            };

            handlerMock
            .Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(response);
            var client = HttpClientHelper.NewTestClient(handlerMock.Object);
            var proxy  = new ApiProxyTestClass(client);


            var retrievedData = await proxy.GetLocationAsync(client.BaseAddress.ToString());

            //Assert.NotNull(retrievedData);
            Assert.True(retrievedData.Any());
            Assert.Equal(json, retrievedData);
        }
Ejemplo n.º 2
0
        public async Task WhenTheCallIsSuccessfulThenWeShouldReceiveAListOfStrings()
        {
            var    handler  = FakeHttpMessageHandler.NewMock();
            string jsonData = @"{'City': 'Dublin'}";

            handler.SetupSuccessfulCall(new List <string> {
                jsonData
            });
            var client = HttpClientHelper.NewTestClient(handler.Object);

            var proxy  = new ApiProxyTestClass(client);
            var result = (await proxy.GetLocationAsync(client.BaseAddress.ToString()));

            Assert.Contains("Dublin", result.ToString());
            Assert.NotNull(result);
        }