Clean mocking for unit tests using NSubstitute in .NET (Core, Framework, Standard)

แชร์
ฝัง
  • เผยแพร่เมื่อ 28 ม.ค. 2020
  • Become a Patreon and get source code access: / nickchapsas
    Check out my courses: dometrain.com
    The giveaway is now over. Thanks for participating.
    Hello everybody I'm Nick and in this .NET tutorial I will show you how you can get started with mocking external dependencies in unit testing using NSubstitute. NSubstitute is, in my opinion, the best mocking framework for unit tests in .NET and I highly recommend it.
    NSubstitute detailed documentation: nsubstitute.github.io/help/ge...
    In object-oriented programming, mock objects are simulated objects that mimic the behavior of real objects in controlled ways, most often as part of a software testing initiative. A programmer typically creates a mock object to test the behavior of some other object, in much the same way that a car designer uses a crash test dummy to simulate the dynamic behavior of a human in vehicle impacts.
    Don't forget to comment, like and subscribe :)
    Social Media:
    Follow me on GitHub: bit.ly/ChapsasGitHub
    Follow me on Twitter: bit.ly/ChapsasTwitter
    Connect on LinkedIn: bit.ly/ChapsasLinkedIn
    #dotnet #tests #mocking

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

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

    Are you looking for more training? Check out my "From Zero to Hero: Unit testing in C#" course: nickchapsas.com/p/from-zero-to-hero-unit-testing-in-c

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

      can you share a disccount code may I ask? Thanks.

  • @digitalfriday
    @digitalfriday 4 ปีที่แล้ว +12

    Excellent introduction to NSubstitue! Not only are your explanations pedagogical, but you also communicate clear and concise intent & flow of information with your code - clear code which speaks for itself. Well done.

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

    I'm just amazed from clear syntax of NSubstitute. Thanks for the video

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

    20 minutes of great training. No fluff, no BS, just enough to solve my problem.

  • @maxbeber
    @maxbeber 4 ปีที่แล้ว +4

    Totally agree that the code looks very clean. I have just started a new project and I am considering to rewrite all my tests using NSubstituite. Keep it up the good job!

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

    This is awesome! Thank you for a great video on Nsubstitute and mocking

  • @habanerohoncho
    @habanerohoncho 3 ปีที่แล้ว

    Thank you, Sir! Clear, concise and well explained.

  • @janoslaszlo3272
    @janoslaszlo3272 3 ปีที่แล้ว

    Very nice mocking framework. I'm going to try it and probably use it from now on.

  • @hmmer3471
    @hmmer3471 4 ปีที่แล้ว

    This is useful, I will try it tomorrow

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

    Excellent video, Thank You.

  • @mohamedelbasmy5160
    @mohamedelbasmy5160 4 ปีที่แล้ว

    GREAT VIDEO. THANK YOU SO MUCH !!

  • @CutieHoney
    @CutieHoney 11 หลายเดือนก่อน

    Wow, your presentation has changed so much since you did this video. Still, very informational in light of current events.

  • @serhiihorun6298
    @serhiihorun6298 4 ปีที่แล้ว

    Thanks again!

  • @fifty-plus
    @fifty-plus 9 หลายเดือนก่อน

    AutoSubstitution with Theories makes testing so much nicer.

  • @arthurmolinari3262
    @arthurmolinari3262 3 ปีที่แล้ว

    Awesome video.

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

    great intro in the framework. But I can't help myself: what most unit test demos show is, that the 1.) the compiler and the framework works 2.) the author can duplicate the behavior in its code into a unit test or the other way around. I would only test stuff that is really of interest. In this case the most important test would be that calling the hand crafted SQL code (which can not be verified at compile time) at least executes and that it executes with a reasonable performance. Unit tests and test driven development should be performed, if only for the reason, that it makes testing new code SO much more efficient. But I can see those management slides that show that we have 10.000 unit test with 1.000.000 lines of code - which suggest the quality has to be great. But it actually doesn't. The tests need to go to the interesting parts and not just consist of a bloat of trivial stuff.

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

      Totally agree. You should test major behaviors, not minor implementation details. The tests shown in the video are basically useless. But they serve as a demo of the framework, which was the intention here of course.

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

    Hey mate, great video.
    You should've explained all the CRUD process, not just the read, that would've been awesome mate.
    Could you please make a video explaining how to mock the Create and delete process?

  • @ivogluhchev
    @ivogluhchev 8 หลายเดือนก่อน

    I'm new to NSubstitue (coming from Moq), the basic concepts are very well explained, congrats. But I would like you to share your approach on mocking HttpClient because as far as I found out this is not a straight forward process. Thanks for the video.

  • @alfonsobarona
    @alfonsobarona 4 ปีที่แล้ว

    Great video! One question: how would you pass the Nsubstitute dependencies to your system under test if the SUT were to be instantiated using IClassFixture for parallelization of the test execution?

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

    Nice job Nick, I am wondering how to unit-test transactions in Entity Framework Core with NSubstitute or Moq when I am trying to unit-test a method that include code with _context.BeginTranscnction( ) ?

  • @sunkanmiijatuyi8118
    @sunkanmiijatuyi8118 4 ปีที่แล้ว

    @Nick Is there a way to test a public access modifier method that has a private access modifier method or static method inside itself?

  • @serhiihorun6298
    @serhiihorun6298 4 ปีที่แล้ว

    Thank u

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

    What should do I if I have extension methods (third party library) inside a method that I want to test?

  • @divyap9698
    @divyap9698 8 หลายเดือนก่อน

    Hi ....is there any way to mock a static method without writing a wrapper or changing the code flow?

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

    Hello, you recently reply to my comment about the triple a's.
    I manage to create a snipped but when I insert the snipped, the first line is formatted correctly but the "// act" and "//Assert" are inserted at beginning of the line, in other word these lines doesn't get formatted.

  • @djidjohoc.agboton7515
    @djidjohoc.agboton7515 3 ปีที่แล้ว

    Hi, do you have any sample using Nsubstitute in VB.NET? Thanks for sharing. Best regards!

  • @douglasproglima
    @douglasproglima 9 หลายเดือนก่อน

    How to test protected methods?

  • @balaji-r-93
    @balaji-r-93 3 ปีที่แล้ว

    Hey Nick, Thank you so much for these videos. You really make developers better and more efficient. I have a question on this, Will NSubstitute work with a Refit Interface which doesn't have an in house implementation? Could you clarify on that? I am breaking my head unit testing Refit interfaces with Moq and it doesn't work for me. Thanks in advance!

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

      It would yes. I use Refit with NSubstitude and it works like any other interface.

    • @balaji-r-93
      @balaji-r-93 3 ปีที่แล้ว

      @@nickchapsas Awesome, thank you!

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

    Is it possible for data access methods in ado.net

  • @user-hf2yv2ve7v
    @user-hf2yv2ve7v ปีที่แล้ว

    Can we used the solution for NUnit?

  • @sunilanthony17
    @sunilanthony17 4 ปีที่แล้ว

    Nick, did you upload this project to GitHub yet? Trying to look for it but can't find it.

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

      Hey, it is uploaded under NugetPackageTutorials/MockingTutorial/CustomersApi.NSubstitute/

    • @sunilanthony17
      @sunilanthony17 4 ปีที่แล้ว

      @@nickchapsas Thank you. I've always had a QA team test my application. Now I'm trying to wrap my head around writing Unit Testing but I can't seem to understand testing DB CRUD operation. I understand to test my application algorithm but testing DB CRUD operation just confuses me.

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

    Prediction: next video is about FluentAssertion :)

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

      I don’t know if it’s gonna be the next one because I wanna cover FakeItEasy as well but it will probably be the one after the mocking ones

  • @Layarion
    @Layarion 4 ปีที่แล้ว

    came here hoping to learn how to use Returns on a void method.

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

    How can we get the source code ?? I desparatly need it.

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

      The source code is available to my Patreons

  • @trongphan6197
    @trongphan6197 4 ปีที่แล้ว

    hi looking for this source code in invited source

    • @nickchapsas
      @nickchapsas  4 ปีที่แล้ว

      It is under NugetPackageTutorials/MockingTutorial/CustomersApi.NSubstitute/

    • @sanjeevbr4676
      @sanjeevbr4676 3 ปีที่แล้ว

      @@nickchapsas I'm sorry I'm new to your channel, Where do I find this path which you have mentioned above?

  • @Ram92026z
    @Ram92026z 7 หลายเดือนก่อน

    Man, I was waiting for you to write the test case for Create which you skipped.