How To Mock HttpClient in C# Unit Tests

Поделиться
HTML-код
  • Опубликовано: 8 янв 2025

Комментарии •

  • @BrentDowdy-k4n
    @BrentDowdy-k4n 5 месяцев назад

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

  • @ashishdasbiswas6587
    @ashishdasbiswas6587 2 месяца назад

    Awesome, just looking for this. U got a new subscriber today !!

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

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

  • @n0block
    @n0block Месяц назад

    Thanks for the video. It really helped.

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

  • @stefanbogdanovic590
    @stefanbogdanovic590 2 года назад

    Exactly what I was looking for! Thanks man :D

  • @SuperTenja
    @SuperTenja Год назад

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

  • @lewispil3529
    @lewispil3529 2 года назад +1

    great video, l learned a lot!

  • @carstenvoss6169
    @carstenvoss6169 8 месяцев назад +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  8 месяцев назад

      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.

  • @burakb8708
    @burakb8708 Год назад +1

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

  • @ShwetaRaut-z3u
    @ShwetaRaut-z3u 10 месяцев назад

    what to do for recursive api call

  • @Micosek98
    @Micosek98 Год назад

    Thanks for help, you are great :D

  • @JoelStrait-j9m
    @JoelStrait-j9m 6 месяцев назад

    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?

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

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