Skip to content

Caleb9/MockingHttpClientExample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A quick example of swapping HttpClient in integration tests for ASP.NET Core application using built-in DI container. By integration tests I mean tests using the Microsoft.AspNetCore.Mvc.Testing library.

Basically it boils down to:

  1. Using HttpClient in an explicit service class. When injecting HttpClient directly into an ASP.NET Core controller class, replacing the client does not work.
  2. Over-registering HttpClient for the service in tests with .AddHttpMessageHandler<T>() extension method. The actual test double must be implementing a DelegatingHandler - NOT an HttpClient.

This technique can be quite useful in integration testing. This example serves as a memo to myself how to achieve this.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages