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 WhenTheCallToTheApiFailsThenWeShouldThrowAServiceFailedException()
        {
            var handler = FakeHttpMessageHandler.NewMock();

            handler.SetupFailedCall();
            var client = HttpClientHelper.NewTestClient(handler.Object);

            var proxy = new ApiProxyTestClass(client);

            await Assert.ThrowsAsync <ServiceFailedException>(() => proxy.GetValues());
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
        public async Task WhenTheCallIsSuccessfulThenWeShouldReceiveAListOfStrings()
        {
            var handler = FakeHttpMessageHandler.NewMock();

            handler.SetupSuccessfulCall(new List <string> {
                "test1", "test2"
            });
            var client = HttpClientHelper.NewTestClient(handler.Object);

            var proxy = new ApiProxyTestClass(client);

            var result = await proxy.GetValues();

            Assert.NotNull(result);
            Assert.True(result.Any());
            Assert.Equal("test1", result.First());
            Assert.Equal("test2", result.Skip(1).First());
        }