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); }
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); }
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")); }
public async Task MethodAsync() { var mock = new MockHttpClient(); mock.Setup(HttpMethod.Get, "/").ReturnsAsync(201); var result = await mock.Object.GetAsync("/"); }
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); }
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); }
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); } }