Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Windows Phone TDD Part 3: Writing the First Test

0.00/5 (No votes)
31 Jan 2014 1  
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.<br />
—-> 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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here