Monday, September 10, 2018

Beginner's Guide To Unit Testing In Prism (Xamarin.Forms)

Overview


As requested i am creating a simple guide to unit testing in Prism for Xamarin.Forms. This is a Step by step guide that you can follow or as your reference in creating your own unit test for Prism project.

What is Unit Testing


It is when you write a code to test your code (sounds like inception huh). This is to ensure the quality and stability of the code and it serves as an "early warning device" when something fails or breaks in our algorithm or logic.


Following AAA's


It is when you write a unit test in Arrange, Act, Assert order. Where "Arrange" is where you set your mocks, initial and expected values, and "Act" could be the actual method you want to invoke and lastly the "Assert", it is where your expected stuff get validated.


So without further ado

Create a Unit Test Project


Keep in mind that the libraries used to demonstrate unit testing in this section rely on .NET Standard 2.0 (which is our XF project) and .NET Core 2.0 project (Our unit test project). 

Assuming we already have existing (.NET standard) xamarin forms project. We will now create a .NET Core unit test project, In this case we will choose MSTest. 





By default, we will be provided with a blank unit test class, we will need to delete it.



Add Reference


You can now add the project reference, and we should now be able to start coding our test. A minor spoiler alert though, you may encounter "Warning" issues on some libraries (e.g. Sqlite etc), it is just ok, you will still be able to run the test. A reference on how to remove annoying warning alert in this LINK





Mocking with Moq


Before we start coding our test, we will gonna need a mocking framework to be able to mimic the interface or classes behavior. 

This isolates the code you’re testing, ensuring that it works on its own and that no other code will make the tests fail.





We will later be able to use it on creating our test method so stay tune, For more about MOQ please check on this LINK


Test Class


We will start by creating a test class. And we will gonna create a test named "MainPageViewModelTest" which will test the MainPageViewModel class. For MSTest we add the [TestClass] annotation to our class.





Test Method


And for test method we add [TestMethod] annotation, this is where the testing logic is executed or validated. 




Test Initialize and Test Cleanup


To complete our ingredients of testing, We gonna need [TestInitialize] and [TestCleanup]. to initialize objects  we use  [TestInitialize] annotation and  [TestCleanup] to cleanup or dispose objects.





Test a NavigatePageCommand


Now lets start writing our test, this time we are testing NavigatePageCommand and we validate it if it does get called.

We will need to initialize the mock references and then we could start working on our test. Below a complete sample.




Dissecting a test



NavigatePageCommand Test



AddTodoCommand Test


Now lets run our Test


You have the option to debug your test if you want to check the objects value, or you can just run it. I will show a popup of the test result when the test execution is done.




Awesome


For more info about MSTEST here is the LINK, and get you started more on Moq here is good resources HERE.



Wrapping Up



If you have some questions go shoot me a message here and will gladly answer your questions. If you want more and advance topic you can catch us at this facebook groups MondPH And Xamdavao. If you want the full sample source you can check it right here LINK. enjoy coding.





1 comment: