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
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
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:
public class MainViewModelTests
public void page_name_should_not_be_empty()
var viewModel = new MainViewModel();
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
Screen and see what happens:
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:
The type initializer for ‘Caliburn.Micro.ViewAware’ threw an exception.<br />
—-> System.IO.FileNotFoundException : Could not load file or assembly
‘System.Windows, Version=184.108.40.206, 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
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