As you know I haven’t been able to unit test my ViewModels since I updated to the beta tools. With the April CTP I was able to use a c# class library with standard NUnit, updated system references and ReSharper to run the tests. After updating to the beta tools I got the following errors:
When trying to update the system dlls to the beta:
“Module ‘System. Version=220.127.116.11, Culture=neutral, PublicKeyToken=7cec85d7bea7798e’ should be referenced.”
From ReSharper when attempting to run tests after manually editing the project file to update the system dlls:
“Unit Test Runner failed to load test assembly:
JetBrains.ReSharper.TaskRunnerFramework.TaskException: Could not load file or assembly ‘System.Device, Version=18.104.22.168, Culture=neutral, PublicKeyToken=24eec0d8c86cda1e’ or one of it dependencies. The system cannot find the file specified.”
I scoured the web in search for the answer. At first, I found several references to the Silverlight Unit Test Framework from Jeff Wilcox which looks cool for xaml testing but I just want to test my ViewModels. I’m pretty sure I could have made it work but it was the wrong tool for the job.
Then I found Jamie Cansdale’s blog and his Silverlight NUnit Projects. This is what I was looking for, but would it work with a Wp7 app? Out of the box, no, but with some tweaks yes! Here is what I did.
- Installed the Silverlight NUnit Project templates (may require a VS restart)
- Added a Silverlight NUnit project to my solution – this project template comes with NUnit.Framework dll built with the Silverlight runtime in the lib folder (this is key)
- I also use Moq for mocking in my unit tests, so again I downloaded and referenced the dll compiled for Silverlight
At this point everything is built successfully and a sample test passed, but no references to the WP7 app yet. When I tried to reference the WP7 app I received the following warning -
“The Target Framework version for the project ‘RouteStats.Ui’ is higher than the current project Target Framework version. Would you like to add this reference to your project anyway?”:
“You can only add project references to other Silverlight projects in the solution”:
So I changed the target framework by opening the project properties (right click on project from solution explorer and choose properties) and changed the Target Silverlight Version from “Silverlight 3” to “Silverlight 4”.
- Next I fixed the “You can only add project references to other Silverlight … “ error by altering the project file. This required unloading the project, then right clicking on the project and choosing “Edit [NameOfProjectFile].csproj”.
- Now I could successfully reference the WP7 app. Again everything built and sample test passed.
- At this point, I added my old tests and the references they require and everything worked great.
That wasn’t to bad right? I’m just glad its over and I can continue using TDD for my ViewModels (MVVM rocks!). I don’t think many developers have had problems with running unit tests against their Windows Phone apps because they haven’t had tests that require references to the phone system dlls. I was testing my geo-location code which requires a reference to System.Device.
Hopefully someone can use this post to get their tests running as well.
Please leave feedback in the comments so I know if any of this is confusing or isn’t working for you. I haven’t tested all areas of my app yet or used all the features of the phone so its possible that this solution won’t always work. I will cross that bridge if I find it exists.