.NET MAUI and Unit Tests

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today I will talk about testing and unit testing and we'll focus on how to test your dotnet Maui View models [Music] welcome back to another video about the dotnet Maui today we will focus on testing and unit testing something that I'm a big fan of I think that writing unit test is really important to ensure a quality of your applications doesn't matter if it's app a backend or a website units tests are there to secure the quality of the code so today we will focus on view models because view models can be a bit tricky how to unit test and I also read a question on Twitter a couple of days ago where one person asked how to best test the view models so I decided to share my Approach in a video so let's go to visual studio and we will start writing some tests so here I have now open Visual Studio and the one of my projects I'm working on it's a dotted malware app and using mvvm so we have the view models here that we can go and test and all this code is already available on GitHub so if you go to my GitHub it will find it and also add a link in the video description but what we need to do now is to go and create a unit test project so we do that new project so we go to test so we can shows between Ms Test X units and N unit I used to use either X unit or n unit but for this demo we can go with the X unit because that is the one I have used most recently here we have the project we got one test from start and we got some Global use also in here so what we need now what I so what I used to do is that I used to use a mocking library for example and a substitute or moq for this we will use and substitute and I also like to use a package called fluent assertion that makes the assert part a little bit easier so we will install those packages now and I will show you how they will work and substitute and fluent assertion okay now we have installed the nuget packages now we are going to create our first test so if we open here we can see what view models we have we have a connect view model for example we can start with that one so we rename this test to call next view model tests and also rename the file because that makes things looks nicer so now we have our first test let's see what we can start with so testing so we have some logic here in in the on a pairing method we have on parameter set we have some commands so I usually like to test those methods here that loading data and are public so like on appearing on parameter set and the commands then we can do some assertions on let's say on parameter set and see that we have the right properties set and with the right values so on parameter set and on appearing here is from the base class I have here and that is from my library tiny MBM but everything I show here can also be used on other Frameworks so let's go to unit test set test because we start with that test so so the first thing we should do is to set up this unit tests so we will follow the triple a pattern our launch act and assert so let's start with arrange so we'll create the view model here and that will be the connect that will be the connect view model that cannot be found here because we also need to go and add a project reference of course to this other project and now it could not restore the packages for that and the reason for that is that we only targeting Mac Catalyst here so we need to go and set also.net 7 plus o as a Target framework like this now we can verify that this still bills so as you can see the build failed and the reason is that we don't have a static main method for net 7. and the reason this fails it's because output type is EXE and to make this work we need to have it as a library so we are going to add some conditions here so we say if Target framework is Mac Catalyst use exe otherwise if it's dotnet7 use library and now we can try again and it should work for us yes now the bill succeeds you can go back here and we should be able to restore the packages Okay so let's see if we can find the view model now yes if you import a namespace set it would not be a problem and we can move this to the use things so global so we don't have to add this every time okay let's see what this one needs we need a eye connection service and we need a future service so let's go and create that connection service and now we will use and substitute to create a substitution object for the interfaces so we do like this sub stitute for I connection service and then we have the feature service so we copy this and change the values feature service and I feature service and now we can add them here that and now we want to test this on parameter set method and as you can see here we don't interact at all with the services so that means that we don't have to create any mocks for them but as you can see here we have a navigation parameter property that should be a string and it checks for the value new so we go to set up that too so we do view model Dot navigation parameter and that is a public property so we can modify it so we just going and set to new and then we can do the actual X part of the test and in this case it just to run a view model on parameter set like this and that is Task so we can go and change the type of the test async charge and then we can await okay now we have that and we should not do anything at all with the result there so what we are going to do now is we go to the third part of the test so you say view model show new and now it comes to the fluent assertions part so we can go and add those usings to Global using fluent assertions so shown you dot so shown you should be true and also show connections were set in that method so we going to test that to show connections should be true No it should be false so now we can run our test so we go here to test Explorer and we can run the test oh it is not working let's see why then okay could not load file or assemble Microsoft Maui controls okay even if we don't have any referenced malware here it seems like it need to have the Maui stuff referenced so what we do is that we go to the project file and here we say use Maui true and now you can try to build it that works fine so then run yeah and now the tests succeed we can go and copy this test and of course we should test what happened if we send in another value here too on parameters that test underscore two just to name it something so we change here to old I don't know why old but we need another value and now both of those should be false and yes they are okay so now we can go and test the on a pairing method so we go to the connect view model and see what we are doing there so what we are doing is that we're loading a list of connections so then we should test if connections probably have a specific amount of values so let's go back to the unit tests we can copy this to get started faster and now we say on pairing test we still need those we need those we don't need that one we should call on appearing and we remove those for now okay so what we need to do now is to specify how many connections that should be returned so connectionservice dot get and then returns and this m substitute feature that we can do like this returns and then say task Dot from result because this is async Method so we need to do like this and now new list of connection and now we can say create connection info object uh if you can create them two M2 ones we don't need the values right here right now and now we can go to assert and say view model Dot connections dot should have count two let's see if we did something else yes show connections should also be true so we check that too thank you should be true so and now we can go and run the on a pairing test yeah it succeeds okay now we want test left to do in this video we also want to test uh command so you go here and see what we have we have this one called open connection so let's go back to the test class and create a new test so copy this one so open connection command test okay so let's see what we need we don't need this we still need that we should call here instead open connection command dot X execute and then we pass null to it and then we don't should not have any weight okay and when we do a search let's see what should we assert on then we can go here so we want this navigate 2 method to be executed and in this case we have an abstraction of the navigation from time MBM but you can use your own there too so let's go here and receive D dot in order async button action add that method and we don't have navigation like this here so we do view model dot navigate shown and then we instead do view model dot connection string here now we just need a semicolon there and we are ready to run this test to see if it works okay so this fails so let's see what it is object reference not set to an instance over object okay so let's debug it uh is it in brit okay here we can see Microsoft model application model not implemented okay maybe we cannot use main thread here one in tests so we should avoid that in the view models and one fix for this is that we can create a wrapper around it so let's do that so we go here we can create helpers folder we create the main thread helper new class let me make it static create a static method called public static void begin in Vogue on Main thread action and then we check if device info dot platform is platform Dot is is equal to device platform dot unknown then instead we just do a task dot run because it doesn't make sense if we do that or not then we do return and then we can do main thread but begin invoke command thread and pass an action and you should leave a comment here that this is for for being unit tests and now we also need this is main thread property so bull is Main thread and get uh we can copy this logic but we instead return true here and here we return main thread Dot is mentored just like this and we should do is that another static okay now we can go back to the connection view model and we can replace this main thread dot is main thread with main thread helper dot is main thread and here we'd say main thread helper dot begin invoke on Main thread okay now we can run this yes it works okay so let's see if all tests works now okay now we have tested uh some methods that are updating properties in viewmodel we have tested method that's our loading data and we have testing commands so this is often how I doing the tests on my view models so of course you can test more stuff you can set property values and check what happens there but often it's enough to test the commands and methods that are loading data so thank you very much for watching this video if you like it subscribe to my channel like the video and there will be more content about.net Maui and other.net stuff in future as well see you then bye bye
Info
Channel: Daniel Hindrikes
Views: 4,651
Rating: undefined out of 5
Keywords: dotnet, maui, .NET, .NET MAUI, dotnetmaui, testing, xunit, visual studio
Id: NLSSVm5FSLY
Channel Id: undefined
Length: 19min 41sec (1181 seconds)
Published: Fri Nov 18 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.