Unit Testing the Authentication Service (with NUnit and Moq) - FULL STACK WPF (.NET CORE) MVVM #13

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ก.พ. 2025
  • I create a unit testing project for the SimpleTrader domain layer and begin writing unit tests for the authentication service. I use NUnit as my test framework and Moq to mock the authentication service dependencies.
    In this series, I walk through the development of a WPF application from the database layer to the UI layer.
    SOURCE CODE: github.com/Sin...
    FULL PLAYLIST: • Full Stack WPF MVVM

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

  • @faraz-online
    @faraz-online 4 ปีที่แล้ว +3

    No doubt, another matchless one! Thanks Sean!

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

    Thank you so much Sean, this is the only video that helped me understand how to use NUnit and Moq in EF very, very clearly.

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

    I do agree: WONDERFUL tutorials! Extremely informative and not boring at all. Keep going this way..

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

    Fantastic tutorial. Subscribed to your amazing channel. How did I not find you. Pls make more of them :)

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

      Thanks palaksha, more on the way :)

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

    Another excellent tutorial, Sean! This can be done with the standard Unit Test framework as well (I did it just to see if I could :) ). The decorators are different, but the rest is just about the same. The only tests that gave me trouble at first were the void Login ones, where I had to make sure that it was still executing the ThrowExceptionAsync properly and I had to change them from void to async Task. Great for learning though!

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

      Thanks Alex, the standard unit test framework (I think it's called MSTest?) is definitely a solid framework too!

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

      @@SingletonSean Loving all the learning I am doing, keep up your great work!

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

    Yet another useful video, the community thanks.

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

    I love your content, it very easy to follow and you explain everything in easy to understand way :) Keep it up!
    Quick question. In your Login_WithIncorrectPasswordForExistingUsername_ThrowsInvalidPasswordExceptionForUsername() method, you put the exception in a variable, then got the username from it, and then asserted whether both are the same:
    InvalidPasswordException exception =
    Assert.ThrowsAsync(() => _authenticationService.Login(expectedUsername,
    testPassword));
    string actualUsername = exception.Username;
    Assert.AreEqual(expectedUsername, actualUsername);
    As you're only interested in Login() method throwing an InvalidPasswordException, wouldn't it make more sense to replace code above with code below?
    Assert.ThrowsAsync(typeof(InvalidPasswordException), () => _authenticationService.Login(expectedUsername, testPassword));

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

      Thanks Bartek! I think that's a fair argument to ensure each test only tests one thing, which is a good standard to follow.

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

    Hey Sean
    excellent explanation of the unit tests, short and accurate.
    A little note, how are the unit tests for the ViewModels as well as for the Converters performed, since NUnit Test is based on .Net Core and WPF on .Net Framework?
    take care and see you soon with another tutorial

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

      Thanks MGA, glad this was helpful! At least in this tutorial, WPF should be dependent on .NET Core too, so I think unit tests would work on view models. I should probably create a new video for unit testing WPF applications since I really haven't done much WPF unit testing across this channel anywhere 😁

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

    Great job you're doing! I'm a junior developer and your lessons are helping me a lot!
    Would you in the future consider developing an app with PRISM/Light MVVM?

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

      Hey Mark, I haven't gone too in-depth with an MVVM framework for WPF. A lot of people have requested I give it a try and I am a bit curious at this point, so perhaps in the future I'll do a series on one!

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

    How to get a connection string from app config file in a class library project? When I execute migration command "add-migration initial" while trying to access connection string from app config file of my WPF project it throws an exception "Object reference not set to an instance of an object". But when I add the actual value in the options.UseSqlServer("connecionstring") then it works why?

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

      Yep Samir, I ran into the same issue a couple weeks ago when planning to do a video on this. There doesn't seem to be any obvious way around this. I'm still exploring, but I think you will need to hard code the connection string for migrations.

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

      @@SingletonSean Thank you for your reply. I hope you'll share with us when you find out. And one more question. If you are into ASP.NET core as well. You know ASP.NET core comes with Dependency Injdection but when I create a class AppDbContext and inherit from DbContext the migration command doesn't see the DbContext and its throws an error Unable to create and object of type AppDbContext but when I execute the migration command after creating a DbContextFactory class with InDesignTimeFactory it sees this and builds the migration. 😅

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

      My solution was to pull the connection string from an environment variable like this options.UseSqlServer(Environment.GetEnvironmentVariable("SqlConnectionString"))
      Then before I run any migrations, I would set my working project in the Package manager console, then, I would set my connection string to an environment variable, $env:SqlConnectionString= "data source = localdb...etc"
      then execute add-migrations, update-database commands
      This way my infrastructure isnt tied to an app.config from another layer that has no need to know about connection strings

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

    Thanks man