Derik Whittaker



Unit Testing Silverlight from MSTest–Forcing VS to use the right Silverlight Assemblies

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)

  1. Move copies of your desired Silverlight .dlls into a local library folder. I have the following in my folder
    1. System.ComponentModel.DataAnnotations.dll
    2. System.Net.dll
    3. System.Runtime.Serialization.dll
    4. System.ServiceModel.dll
    5. System.ServiceModel.DomainServices.Client.dll
    6. System.Windows.dll
    7. System.Xml.dll
  2. Open up your unit tests .proj file in notepad or any text editor
    1. 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>
  3. Save the .proj file
  4. Open it back up in VS (or reload)
  5. Verify the correct version is not being used by viewing the properties of the dll in VS
  6. 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 Sad smile

Till next time,

Posted 08-26-2011 8:13 AM by Derik Whittaker



James Manning wrote re: Unit Testing Silverlight from MSTest–Forcing VS to use the right Silverlight Assemblies
on 08-26-2011 9:27 AM

Not that it really matters, but I'd guess this is more a limitation in the MSTest framework - the test project is a .NET 4 project, so it builds/runs as .NET 4 (at least, that's my current understanding).  I'd imagine that to have 'true' silverlight unit tests (running the tests in the silverlight runtime), the test runner would need to  instantiate/host the silverlight runtime and then run the tests in that?

Then you'd have the additional complexity of finding out which of the frameworks you use (rhino mocks, typemock isolater, whatever) actually support running within the silverlight framework :)

Frank Mao wrote re: Unit Testing Silverlight from MSTest–Forcing VS to use the right Silverlight Assemblies
on 08-31-2011 10:58 AM

We are using AgUnit 0.4.2 for Resharper 6, unit-testing right in VS2010 works very smoothly, while code coverage still a bit complex.

Tom Philip wrote re: Unit Testing Silverlight from MSTest–Forcing VS to use the right Silverlight Assemblies
on 09-01-2011 5:53 AM

+1 for no thought about testability for Silverlight.

For unit tests (or should I say specs) I've managed to get away with using MSpec in the 4.0 runtime environment. This was mostly succesful as you're verifying business logic. Most problems came with integration tests, when you're touching stuff outside of your own code. I think I used NUnit for Silverlight which helped a lot (not completely, there was always something that catches you out).

And then last but not least I used White for the acceptance tests which ensured features worked. This covered the gaps left by the parts I couldn't get under test (mostly integration).

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Google Reader or Homepage Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of
Red-Gate Tools For SQL and .NET


SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
NHibernate Profiler
Balsamiq Mockups
JetBrains - ReSharper
Web Sequence Diagrams
Ducksboard<-- NEW Friend!


Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers


Community Server (Commercial Edition)