Ok, silverlight and unit testing just plan sucks, I firmly believe that the devs for silverlight did NOT care about testability, but that is off topic. When doing any type of unit testing (and not with the built in silverlight testing framework which actually runs the app, talking code level testing) you will quickly run into an issue of version mis-match.
The issue with the version mis-match is that VS wants to push the 4.0 version of your assemblies, even when you add direct reference to the 2.0.5 version (silverlight version). When this happens your tests will fail with an error telling you that your assemblies are not the right version. And if you are like me you will try over and over to ‘force’ vs to reference the right version and it will fail. The good news is that there IS a way around this, but it is a bit of a hack
How to get around this issue (at least one way)
- Move copies of your desired Silverlight .dlls into a local library folder. I have the following in my folder
- System.ComponentModel.DataAnnotations.dll
- System.Net.dll
- System.Runtime.Serialization.dll
- System.ServiceModel.dll
- System.ServiceModel.DomainServices.Client.dll
- System.Windows.dll
- System.Xml.dll
- Open up your unit tests .proj file in notepad or any text editor
- Find the <Reference> entry to the correct silverlight dll you want and create a hint path which points to your LOCAL dll
See this example:
<Reference Include="System.ServiceModel">
<HintPath>..\..\..\Libraries\Silverlight Test dlls\2.0.5 DLLs\System.ServiceModel.dll</HintPath>
</Reference>
- Save the .proj file
- Open it back up in VS (or reload)
- Verify the correct version is not being used by viewing the properties of the dll in VS
- Get back to testing
As you can see this is fixable, but it is also a bit of a hack/work around. However this little bit of pain is worth the gains of adding tests to your code. Now if only silverlight as a framework was more testable 
Till next time,
Posted
08-26-2011 8:13 AM
by
Derik Whittaker