Monday, September 10, 2018

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


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.


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. Replies
    1. Great Article android based projects

      Java Training in Chennai

      Project Center in Chennai

      Java Training in Chennai

      projects for cse

      The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

  2. It is really very excellent,i find all articles was amazing.awesome way to get exert tips from everyone,not only i like that post all peoples like that post,because of all given information was wonderful and it's very helpful for me.
    Dot Net Training Institute in Chennai
    Software Testing Course in Chennai
    Core Java Training in Chennai
    PHP Certification in Chennai

  3. Amazing Article ! I have bookmarked this article page as i received good information from this. We are the best Mobile App Development Company India.

  4. I will recommend anyone looking for Business loan to Le_Meridian they helped me with Four Million USD loan to startup my Quilting business and it's was fast When obtaining a loan from them it was surprising at how easy they were to work with. They can finance up to the amount of $500,000,000.000 (Five Hundred Million Dollars) in any region of the world as long as there 1.9% ROI can be guaranteed on the projects.The process was fast and secure. It was definitely a positive experience.Avoid scammers on here and contact Le_Meridian Funding Service On. / WhatsApp...+ 19893943740. if you looking for business loan.

  5. Shweta gaur is one of the famous makeup artist in all over India. We are providing the best makeup artist courses and more other courses in over branches in Delhi.
    Bridal Makeup Makeup Artist in Delhi Makeup Artist Best Makeup Artist in Delhi Best Makeup Artist in East Delhi Top Makeup Artist in Delhi Top Makeup Artist in India Bridal Makeup

  6. Woah!! Such a piece of the nice information you have shared here, I have read the entire post and I must say that the information is very helpful for me.
    Hire Xamarin Developer
    Hire Xamarin Development Company
    Xamarin Development Company

  7. The article is so appealing. You should read this article before choosing the Big data cloud analytics you want to learn.

  8. I am very grateful to Elegant loan firm for helping me get a $ 600,000 loan with the helping of loan officer Russ Harry, and I will always be grateful. My life has changed, my money has been paid, I now own a business that I used to support my family. I am grateful to you, Mr. Russ, and God bless you. You can contact them for your financial assistance by e-mail: for your financial assistance.

  9. Nice post! putlocker
    There is more relative topics below-

  10. 'I'm highly impressed by the piece of thoughts you have shared on this portal. all the best
    connect us on Assignment Help can shed your burden of assignments with a return of qualitative assignments.
    Online Assignment Help
    Programming Assignment Help
    Management Assignment Help
    assignment experts
    Networking Assignment Help

  11. It’s an amazing informative post. Thank you for sharing.
    software testing course

  12. Nice Post.. really helpful to clear my small concepts. thanks :)Keep doing more, waiting to read your next blog security testing services. Visit here for more information about Vulnerability assessment services and penetration testing services.

  13. My name is Mr. Klaus Dieter, I work for the French financial institution Crédit Mutuel with the partnership of the bank BNP Paribas, we offer you offers at an interest rate of 3% per year, to start with new projects:
    - You have unpaid debt.
    - Buying a new house.
    - buy a new car.
    - Buying a new apartment or property.
    Get personal loans, business loans.
    If you would like loans for setting up your business and project, do not hesitate to send us an email:

    Whatsapp: +4917694954827

  14. Thanks for sharing this useful info. In22labs (unwind learning labs) is one of the leading E-governance solutions providers in India. We have worked on 100+ portals for Egovernance using the latest technologies. Know more Government apps development providers