Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Windows Phone TDD Part 3: Writing the First Test

, 31 Jan 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
I will show you how to create the project for the unit tests, and I will write a small test to see if it all works.

Introduction

In the previous tutorial, you saw how I installed Caliburn.Micro in my project. Now I will show you how to create the project for the unit tests, and I will write a small test to see if it all works.

The first thing you need to do is to create a new project. I’m going to use a C# class library project…not a Windows Phone class library, not a PCL class library, not a Windows Phone Unit Test App… just a simple C# class library project. The reason I’m choosing this type of project is because you can install any library you want, and it works with NUnit. However, it also has a disadvantage , you can’t make any integration tests, like testing the isolated storage, navigation service, etc.

The project will be called “BudgetPlanner.Tests”. I like to use this name, but you can use whatever you want.

Unit test project setup WP8

Unit test project setup WP8

Remove the file Class1.cs that was created by default in the test project, and add a new one named MainViewModelTests.cs.

After you do this, install NUnit and FluentAssertions from Nuget. The easiest way to do this is to right click on References from the tests project, click on Manage Nuget Packages, and search for NUnit. This window will appear, and here you have NUnit and FluentAssertions.

NUnit + FLuent Assertions packages

NUnit + FLuent Assertions packages

The next step is to add a reference to the Windows Phone 8 project, so right click on References -> Add reference -> Solution -> Check the BudgetPlaner.WP8 checkbox -> OK.

We should have everything in place, so let’s write a simple test, to see if it works. Write this in your MainViewModelTests.cs file:

namespace BudgetPlanner.Tests
{
    using NUnit.Framework;
    using FluentAssertions;

    using WP8.ViewModels;

    public class MainViewModelTests
    {
        [Test]
        public void page_name_should_not_be_empty()
        {
            var viewModel = new MainViewModel();
            viewModel.PageName.Should().NotBeNull();
        }
    }
}

Now run the test. Congratulations, your first test passed!!! Now you’re a TDD developer…NOT!

This test was written just to check if everything works OK. It was not TDD, because if you remember, we wrote the code for the MainViewModel in the previous part, so this was Code First, not Test First Development. Before going in the real stuff, I just want to show you how to fix a problem that you will definitely encounter later.

If you used Caliburn.Micro before, you probably used the Screen class. This class allows you to implement properties with NotificationChange and it has all sort of methods and events that can help you. I will cover this in a later tutorial, but for know you just have to know that you will definitely need it sooner or later, and you use it by making your ViewModel inherit from it. The problem is that if your ViewModel inherits it, then you will have problems running your tests. Let’s make the MainViewModel inherit Screen and see what happens:

caliburn error

caliburn error

As you can see, our test project now needs to reference the Caliburn.Micro library. If you use Resharper, you just have to click on one of the underlined words, and hit ALT+ENTER -> Add reference to Caliburn.Micro, so Resharper will add the reference for you, but you can do it by yourself if you don’t have Resharper. After you add the reference, clean the solution(Build -> Clean Solution) and rebuild it.

Now everything looks OK, there are no more errors, but when you run the test you will notice that the test fails because of this error:

test fail

test fail

System.TypeInitializationException : 
The type initializer for ‘Caliburn.Micro.ViewAware’ threw an exception.

—-> System.IO.FileNotFoundException : Could not load file or assembly 
‘System.Windows, Version=2.0.6.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e’ 
or one of its dependencies. The system cannot find the file specified.

To fix this, you have to add a reference to “System.Windows.dll”. Right click on References -> Add reference -> Browse -> Go to “C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v8.0\Tools\MDILXAPCompile\Framework” -> Find the System.Windows.dll file -> Click on Add -> Click OK.

Add System.Windows reference

Add System.Windows reference

Now you should clean and rebuild the project, and the test should work if you run it again.

That’s it for now. The source code is hosted in a public Bitbucket repository so you can download it from https://bitbucket.org/bogdanbujdea/moneyplanner.

In the next part, we will write the tests for the first feature:

  • As a user, I want to add my monthly budget and set a time period for it

TDD Posts Series

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Bogdan Bujdea
Software Developer Feel IT
Romania Romania
Hey! I'm Bogdan Bujdea, a software developer from Romania. I started to learn programming since my first year of highschool with C++. During my time in college I tried various languages like Java, C#, Haskell, Javascript, Python, etc. but my favorite remains C#.
I started my career with Thinslices as a Windows 8 developer, but now I work for Feel IT Services as a Windows Phone developer.
Follow on   Twitter   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150327.1 | Last Updated 31 Jan 2014
Article Copyright 2014 by Bogdan Bujdea
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid