{"id":756,"date":"2020-09-30T09:05:20","date_gmt":"2020-09-30T09:05:20","guid":{"rendered":"https:\/\/bruceng.com\/dev\/?p=756"},"modified":"2020-09-30T09:05:20","modified_gmt":"2020-09-30T09:05:20","slug":"how-to-mock-httpclient-in-c-for-unit-testing","status":"publish","type":"post","link":"https:\/\/bruceng.com\/dev\/?p=756","title":{"rendered":"How to &#8220;Mock&#8221; HttpClient in C# for unit testing"},"content":{"rendered":"\n<p>Follow this article<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-embed-wordpress wp-block-embed is-type-wp-embed is-provider-gingter-ale\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"B6TnkBMdFN\"><a href=\"https:\/\/gingter.org\/2018\/07\/26\/how-to-mock-httpclient-in-your-net-c-unit-tests\/\">How to mock HttpClient in your .NET \/ C# unit tests<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;How to mock HttpClient in your .NET \/ C# unit tests&#8221; &#8212; Gingter Ale\" src=\"https:\/\/gingter.org\/2018\/07\/26\/how-to-mock-httpclient-in-your-net-c-unit-tests\/embed\/#?secret=B6TnkBMdFN\" data-secret=\"B6TnkBMdFN\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>You cannot mock HttpClient, but you can mock the HttpMessageHandler that the HttpClient uses.<\/p>\n\n\n\n<p>To use in unit tests, make sure HttpClient can be injected into the test class, then in tests create an instance of HttpClient with a mocked HttpMessageHandler.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Follow this article You cannot mock HttpClient, but you can mock the HttpMessageHandler that the HttpClient uses. To use in unit tests, make sure HttpClient can be injected into the test class, then in tests create an instance of HttpClient with a mocked HttpMessageHandler.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[51,7,37],"class_list":["post-756","post","type-post","status-publish","format-standard","hentry","category-tips-and-tricks","tag-net","tag-c","tag-unit-testing"],"_links":{"self":[{"href":"https:\/\/bruceng.com\/dev\/index.php?rest_route=\/wp\/v2\/posts\/756","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bruceng.com\/dev\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bruceng.com\/dev\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bruceng.com\/dev\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/bruceng.com\/dev\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=756"}],"version-history":[{"count":1,"href":"https:\/\/bruceng.com\/dev\/index.php?rest_route=\/wp\/v2\/posts\/756\/revisions"}],"predecessor-version":[{"id":757,"href":"https:\/\/bruceng.com\/dev\/index.php?rest_route=\/wp\/v2\/posts\/756\/revisions\/757"}],"wp:attachment":[{"href":"https:\/\/bruceng.com\/dev\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=756"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bruceng.com\/dev\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=756"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bruceng.com\/dev\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=756"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}