---
title: "Unit Testing a Method That Uses HttpClient"
url: https://daily.dev/posts/unit-testing-a-method-that-uses-httpclient-m2ht8moga
source_url: https://nodogmablog.bryanhogan.net/2017/12/unit-testing-a-method-that-uses-httpclient
type: article
source: "No Dogma Blog (Bryan Hogan)"
published: 2026-05-31T07:46:34.387Z
updated: 2026-05-31T09:02:57.953Z
tags: ["testing", "c#"]
reading_time: 2
upvotes: 0
comments: 0
language: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Unit Testing a Method That Uses HttpClient

**[No Dogma Blog \(Bryan Hogan\)](https://daily.dev/sources/bryanhogan)** · 2 min read · 0 upvotes · 0 comments

## Summary

A practical guide to unit testing an ASP.NET controller action method that depends on HttpClient. Since there is no IHttpClient interface to mock directly, the approach involves mocking HttpMessageHandler and passing it to the HttpClient constructor. The post includes a complete code example using Moq and xUnit, showing how to set up the mock to return a predefined JSON response and then assert on the controller's action result.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://nodogmablog.bryanhogan.net/2017/12/unit-testing-a-method-that-uses-httpclient>

## Similar posts on daily.dev

- [Testing ASP.NET Core Web API: WebApplicationFactory and Integration Tests](https://daily.dev/posts/testing-asp-net-core-web-api-webapplicationfactory-and-integration-tests-8chotq6j5) · We Are .NET · 1 upvotes · 0 comments

---

Tags: [#testing](https://daily.dev/tags/testing), [#c#](https://daily.dev/tags/c#)

[View this post on daily.dev](https://daily.dev/posts/unit-testing-a-method-that-uses-httpclient-m2ht8moga)
