![http client test http client test](https://assist-software.net/sites/default/files/promoted_images/members_articles/postman.png)
Public class Client2Service: IClient2Service (Line: 36-45) So on mocking the 'HttpClient', to check the test more efficient way we have to call the 'MockMessageHandler.Protected().Verify()' method, here we can check whether our 'SendAsync' method is executed or not, and also we can verify the kind of request we invoked and also we can verify the requested URL.Īpi_project/Services/Client2Service.cs: using.(Line: 34) Checking whether response data is null or not.Since we mocked our 'HttpClient' we are going to receive our mock data as a response. (Line: 31) Invoking our actual test method that is 'Get()' in the 'Client1Service'.(Line: 28) Initialized the 'Client1Service' by passing the 'HttpClient' instance as input.(Line: 26) The base domain of the third-party endpoint.Inside of the mocked 'HttpMessageHandler', we handled the 'SendAsync' method execution. (Line: 25) Initialized HttpClient instance bypassing our 'HttpMessageHandler' as input to it.(Line: 22&23) Fetching the mock of 'HttpMessageHandler'.(Line: 21) Fetching the mock response data of type 'PublicApiContainer'.(Line: 17) The 'Fact' attribute makes the method a testable unit.Var results = await client1Service.Get() Var client1Service = new Client1Service(mockHttpClient) MockHttpClient.BaseAddress = new Uri("") Var mockHttpClient = new HttpClient(mockHandler.Object)
![http client test http client test](https://venturebeat.com/wp-content/uploads/2018/05/star-treke284a2_-bridge-crew_20180518211334.jpg)
XUnit_Project/System/Services/TestClient1Service.cs: using System Let's mock some response data, so let's create a file like 'PublicApiMock.cs' in the 'MockData' folder. Here 'ItExpr' matches any input parameters of a protected method like 'SendAsync'. Here we going to mock the 'SendAsync' method with our mocked 'HttpResponseMessage' as output.