WireMock-Net / WireMock.Net

WireMock.Net is a flexible product for stubbing and mocking web HTTP responses using advanced request matching and response templating. Based on the functionality from http://WireMock.org, but extended with more functionality.
Apache License 2.0
1.41k stars 209 forks source link

Unit testing for external API #799

Closed kiquenet closed 2 years ago

kiquenet commented 2 years ago

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

StefH commented 2 years ago

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 2 years ago

@kiquenet is this clear?

kiquenet commented 2 years ago

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

StefH commented 2 years ago

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