

To download the source code for this article, you can visit our GitHub repository. We will use Moq to mock a HttpMessageHandler, and also, we will analyze mocking HttpClient by using the MockHttp NuGet package.
Http client test how to#
The lightweight and fast nature of httptest makes it an easy way to. In this article, we will show you how to mock HttpClient and compare two ways to mock HTTP requests in our unit tests. The httptest package solves this problem by mocking requests and responses. Mock.When("/getData").RespondJson(new List, serialized as JSON. To test a server, you need a client and vice versa. With the helper methods in place, you can do the following in your tests: using var ctx = new TestContext() Response.Content = new StringContent(JsonSerializer.Serialize(contentProvider())) Public static MockedRequest RespondJson(this MockedRequest request, Func contentProvider) = new MediaTypeHeaderValue("application/json") Response.Content = new StringContent(JsonSerializer.Serialize(content)) Var response = new HttpResponseMessage(HttpStatusCode.OK) Public static MockedRequest RespondJson(this MockedRequest request, T content) HttpClient.BaseAddress = new Uri(" services.AddSingleton(httpClient) Var httpClient = mockHttpHandler.ToHttpClient() Var mockHttpHandler = new MockHttpMessageHandler() Public static MockHttpMessageHandler AddMockHttpClient(this TestServiceProvider services) Public static class MockHttpClientBunitHelpers It makes it easier to add the HttpClient mock toīUnit's test context's Services collection, and configure responses to requests: using Bunit The following extension class to your test project. To make it easier to work with RichardSzalay.MockHttp, add To use RichardSzalay.MockHttp, add the following package reference to your test project's. RichardSzalay.MockHttp we can easily add one that works There is currently no built-in mock for HttpClient in bUnit, but with the use of NET Core is a bit more cumbersome than interface-based services like IJSRuntime. To be even more clear, when the flush statement is executed, it sends the mockBookArray data through the stream, the subscribe block resolves and our assertion then takes place.Īt this point, if you run the test, you should get a passing checkmark.Mocking the HttpClient service in. However, because we must subscribe to the getAllBooks function, we flush the data after we're listening for that Observable to emit the value. At first glance, this sort of seems to go against the normal testing pattern where you'd specify the data to be returned before the assertion statement.

We also use the HttpTestingController to flush (send) data through the stream.We set up and utilize the HttpTestingController for multiple reasons, but here we're using it to specify the URL that we expect the Service function to hit, as well as the request method to be used.

You may be thinking that this looks funny why do we need to subscribe to the getAllBooks function? The function returns an Observable, so the only way to check the data that is being returned is to subscribe to the Observable and make the assertion inside. This satisfies the Assert step in the Arrange-Act-Assert pattern. I make sure the data coming back from the function is an array of Books, which I've mocked out and brought into this test file.This is part of the Act step in the Arrange-Act-Assert pattern. I call the code under test - the getAllBooks function.This may look like a lot and be confusing, so let me break it down.
