How To Mock HttpClient in C# Unit Tests

แชร์
ฝัง
  • เผยแพร่เมื่อ 14 ต.ค. 2024
  • How to mock the c# http client when for your unit tests.
    Patreon 🤝 / raw_coding
    Courses 📚 learning.raw-c...
    Shop 🛒 shop.raw-codin...
    Discord 💬 / discord
    Twitter 📣 / anton_t0shik
    Twitch 🎥 / raw_coding
    #aspnetcore #httpclient #csharp

ความคิดเห็น • 25

  • @BrentDowdy-k4n
    @BrentDowdy-k4n 2 หลายเดือนก่อน

    Crushed it, I give this to anyone that asks about HttpClient. Thanks!

  • @MayronWoW
    @MayronWoW 2 ปีที่แล้ว +11

    I would say your HttpMessageHandlerMock class is more of a Stub than a Mock. A stub is a simple fake object with the data you need to pass a test, whereas mock's are usually smarter, such as allowing you to verify calls against them. I'd rename it to HttpMessageHandlerStub.

    • @RawCoding
      @RawCoding  2 ปีที่แล้ว +8

      Yea I avoid that type of terminology, fake,stub,mock etc.. are all mocks to me

  • @sajagjain7169
    @sajagjain7169 2 ปีที่แล้ว

    This is Epic. I just got a notification for this. I have meaning to get to know this for quite sometime now

  • @stefanbogdanovic590
    @stefanbogdanovic590 2 ปีที่แล้ว

    Exactly what I was looking for! Thanks man :D

  • @ivanvincent7534
    @ivanvincent7534 2 ปีที่แล้ว +3

    I heard on another video that it’s best to use the Http factory to create clients.. supposedly it handles potential memory leaks better... With that in mind would you say it’s possible to mock a factory to create clients for testing?

    • @RawCoding
      @RawCoding  2 ปีที่แล้ว +2

      You’d make the factory return an instance as described in the video.

  • @carstenvoss6169
    @carstenvoss6169 5 หลายเดือนก่อน +1

    I am asking myself, what the test tests. In the second example, you test that the HttpClient returns a response that you provided

    • @RawCoding
      @RawCoding  5 หลายเดือนก่อน

      I am showing how to mock an httpclient for testing, I am not testing anything so you shouldn’t be asking yourself that because the video is not about writing a test.

  • @lewispil3529
    @lewispil3529 ปีที่แล้ว +1

    great video, l learned a lot!

  • @Micosek98
    @Micosek98 ปีที่แล้ว

    Thanks for help, you are great :D

  • @burakb8708
    @burakb8708 ปีที่แล้ว +1

    I don't get why we need to delve into the specifics of http client for our tests.

  • @SuperTenja
    @SuperTenja ปีที่แล้ว

    Hey, can we get the code repo for the above (it is not there in the description). Thanks!

  • @JoelStrait-j9m
    @JoelStrait-j9m 3 หลายเดือนก่อน

    Wondering how to copy the source code from this video? I am trying to understand mocking, confusing.

  • @arkkism
    @arkkism 2 ปีที่แล้ว +1

    Have you considered using Moq?

    • @MayronWoW
      @MayronWoW 2 ปีที่แล้ว +6

      Moq only allows you to mock interfaces, not concrete types, and HttpClient is an old artifact of .net which has no interface.

    • @mteeum
      @mteeum 2 ปีที่แล้ว

      var handlerMock = new Mock(MockBehavior.Strict);
      handlerMock
      .Protected()
      .Setup(
      "SendAsync",
      ItExpr.IsAny(),
      ItExpr.IsAny()
      )
      .ReturnsAsync(new HttpResponseMessage()
      {
      StatusCode = HttpStatusCode.NoContent
      });
      This works :)

  • @bo44arov
    @bo44arov 2 ปีที่แล้ว +2

    Why don't we just wrap httpClient into some httpService and then mock it?

    • @RawCoding
      @RawCoding  2 ปีที่แล้ว +1

      Even if you wrap infinitely down below there will still be an httpclient you’ll need to test.

  • @Kevinob
    @Kevinob 2 ปีที่แล้ว

    Hi, thanks, where is the repo in your github?

    • @RawCoding
      @RawCoding  2 ปีที่แล้ว

      Check description

  • @ShwetaRaut-z3u
    @ShwetaRaut-z3u 7 หลายเดือนก่อน

    what to do for recursive api call

  • @assafkerenak
    @assafkerenak 2 ปีที่แล้ว +3

    Font too small, hard to see what you write

    • @RawCoding
      @RawCoding  2 ปีที่แล้ว +5

      🔎

    • @assafkerenak
      @assafkerenak 2 ปีที่แล้ว +5

      @@RawCoding on smartphone it's very inconvenient,I would appreciate if you try bigger font