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!
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
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 😁
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));
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?
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!
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?
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.
@@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. 😅
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
No doubt, another matchless one! Thanks Sean!
I do agree: WONDERFUL tutorials! Extremely informative and not boring at all. Keep going this way..
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.
Yet another useful video, the community thanks.
Fantastic tutorial. Subscribed to your amazing channel. How did I not find you. Pls make more of them :)
Thanks palaksha, more on the way :)
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!
Thanks Alex, the standard unit test framework (I think it's called MSTest?) is definitely a solid framework too!
@@SingletonSean Loving all the learning I am doing, keep up your great work!
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
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 😁
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));
Thanks Bartek! I think that's a fair argument to ensure each test only tests one thing, which is a good standard to follow.
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?
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!
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?
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.
@@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. 😅
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
Thanks man