How To Mock HttpClient in C# Unit Tests

Поділитися
Вставка
  • Опубліковано 15 жов 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

  • @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.

  • @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.

  • @burakb8708
    @burakb8708 Рік тому +1

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

  • @lewispil3529
    @lewispil3529 Рік тому +1

    great video, l learned a lot!

  • @JoelStrait-j9m
    @JoelStrait-j9m 3 місяці тому

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

  • @Micosek98
    @Micosek98 Рік тому

    Thanks for help, you are great :D

  • @SuperTenja
    @SuperTenja Рік тому

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

  • @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.

  • @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 :)

  • @ShwetaRaut-z3u
    @ShwetaRaut-z3u 7 місяців тому

    what to do for recursive api call

  • @Kevinob
    @Kevinob 2 роки тому

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

  • @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