Unit testing for external API #442

Closed
opened 2025-12-29 15:23:52 +01:00 by adam · 4 comments
Owner

Originally created by @kiquenet on GitHub (Aug 31, 2022).

How-to do unit testing for external API (like Fake API ) api.chucknorris.io ?

simple-example-of-calling-rest-api-with-httpclient-in-net5.0

Originally created by @kiquenet on GitHub (Aug 31, 2022). How-to do unit testing for external API (like Fake API ) api.chucknorris.io ? **[simple-example-of-calling-rest-api-with-httpclient-in-net5.0](https://adamstorr.azurewebsites.net/blog/simple-example-of-calling-rest-api-with-httpclient-in-net5.0)**
adam added the question label 2025-12-29 15:23:52 +01:00
adam closed this issue 2025-12-29 15:23:52 +01:00
Author
Owner

@StefH commented on GitHub (Aug 31, 2022):

Please see https://github.com/WireMock-Net/WireMock.Net/wiki/Using-WireMock-in-UnitTests

In your case

var joke = new ChuckNorrisJoke
{
};

_server
    .Given(Request.Create().WithPath("/jokes/random").UsingGet())
    .RespondWith(
      Response.Create()
        .WithStatusCode(200)
        .WithBodyAsJson(joke)
    );
@StefH commented on GitHub (Aug 31, 2022): Please see https://github.com/WireMock-Net/WireMock.Net/wiki/Using-WireMock-in-UnitTests In your case ``` c# var joke = new ChuckNorrisJoke { }; _server .Given(Request.Create().WithPath("/jokes/random").UsingGet()) .RespondWith( Response.Create() .WithStatusCode(200) .WithBodyAsJson(joke) ); ```
Author
Owner

@StefH commented on GitHub (Sep 1, 2022):

@kiquenet is this clear?

@StefH commented on GitHub (Sep 1, 2022): @kiquenet is this clear?
Author
Owner
@kiquenet commented on GitHub (Sep 1, 2022): Only useful references: HttpClient and testing https://adamstorr.azurewebsites.net/blog/test-your-dotnet-httpclient-based-strongly-typed-clients-like-a-boss IHttpClientFactory vs HttpClient https://docs.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests [YOU'RE USING HTTPCLIENT WRONG AND IT IS DESTABILIZING YOUR SOFTWARE](https://www.aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/)
Author
Owner

@StefH commented on GitHub (Sep 1, 2022):

Thanks for these references, I know these, but this is not really applicable to WireMock.Net, it;s more applicable to the client code which calls the external api / calls WireMock.Net

@StefH commented on GitHub (Sep 1, 2022): Thanks for these references, I know these, but this is not really applicable to WireMock.Net, it;s more applicable to the client code which calls the external api / calls WireMock.Net
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#442