コード例 #1
0
        public async Task GetById_SetupWithDelegate_SuccessResponse_ReturnsExpectedEntity()
        {
            var id = Guid.NewGuid().ToString();

            var uri = $"{_baseUri}/{id}";

            _httpClientMock.Setup(
                request =>
                request.Method.Equals(HttpMethod.Get) && request.RequestUri.AbsoluteUri.Equals(uri))
            .Returns(new HttpResponseMessage(System.Net.HttpStatusCode.OK)
            {
                Content = new StringContent(JsonConvert.SerializeObject(new SampleEntity
                {
                    Id   = id,
                    Name = "Sample Entity"
                }),
                                            Encoding.UTF8,
                                            "application/json")
            });

            var result = await _sut.GetById(id);

            Assert.That(result, Is.Not.Null);
            Assert.AreEqual(id, result.Id);
        }
コード例 #2
0
        public async Task TestMultipleMocksWithOneSuccessful()
        {
            var mock = new MockHttpClient();

            mock.Setup(HttpMethod.Get, "/api/v2").ReturnsAsync(201);
            mock.Setup(HttpMethod.Get, "/api/v1").ReturnsAsync(203);
            mock.Setup(HttpMethod.Get, "/").ReturnsAsync(200);
            mock.Setup(HttpMethod.Get, "/api").ReturnsAsync(204);

            var result = await mock.Object.GetAsync("/");

            Assert.Equal(200, (int)result.StatusCode);
        }
コード例 #3
0
        public async Task StringAsync()
        {
            var mock = new MockHttpClient();

            mock.Setup <string>(HttpMethod.Post, "/", content: x => x == "test").ReturnsAsync(201);
            var result = await mock.Object.PostAsync("/", new StringContent("test"));
        }
コード例 #4
0
        public async Task MethodAsync()
        {
            var mock = new MockHttpClient();

            mock.Setup(HttpMethod.Get, "/").ReturnsAsync(201);
            var result = await mock.Object.GetAsync("/");
        }
コード例 #5
0
        public async Task StringContentAsync()
        {
            var mock     = new MockHttpClient();
            var expected = 201;

            mock.Setup <StringContent>(HttpMethod.Post, "/", content: x => x == "test").ReturnsAsync(expected);
            var result = await mock.Object.PostAsync("/", new StringContent("test"));

            Assert.Equal(expected, (int)result.StatusCode);
        }
コード例 #6
0
        public async Task ReturnsStringContentAsync()
        {
            var mock     = new MockHttpClient();
            var expected = new StringContent("result");

            mock.Setup <StringContent>(HttpMethod.Post, "/", content: x => x == "test").ReturnsAsync(expected);
            var result = await mock.Object.PostAsync("/", new StringContent("test"));

            var actual = await result.Content.ReadAsStringAsync();

            Assert.Equal(expected, actual);
        }
コード例 #7
0
        public async Task MultipleReturnsSetupAsync()
        {
            var mock   = new MockHttpClient();
            var first  = new StringContent("result");
            var second = 201;

            mock.Setup <StringContent>(HttpMethod.Post, "/", content: x => x == "test")
            .ReturnsAsync(first)
            .ReturnsAsync(second);

            {
                var result = await mock.Object.PostAsync("/", new StringContent("test"));

                var actual = await result.Content.ReadAsStringAsync();

                Assert.Equal(first, actual);
            }

            {
                var result = await mock.Object.PostAsync("/", new StringContent("test"));

                Assert.Equal(second, (int)result.StatusCode);
            }
        }