Setting up a SilverUnit project

To start using SilverUnit you will need:

  • The latest version of Typemock Isolator installed on the local machine (currently it is 5.3)
  • The latest version of the SilverUnit dlls (currently 0.4) : CThru.dll and CThru.Silverlight.dll that were compiled against the latest version of Isolator.
  • A silverlight class library or executable project that you'd like to test (compiled or as source)
  • Visual Studio Express or above
  • NUnit, MbUnit or MSTest

Using the SilverUnit Project Template

  • The simplest solution to start writing a test would be to download the SilverUnit project template for visual studio from the latest release.
  • double click on the downloaded .vsi file and visual studio will create a new project type under "silverlight" for you. More details here
  • if you somehow are not able to do this, then follow the instructions below.

Follow these steps to create a project Manually

  • NOTE: only use these instructions if you are not going to use the project template installer (in the paragraph above).
  • Create an empty solution in visual studio
  • Add the Silverlight project that you'd like to test to the solution projects
  • Create a new standard (not Silverlight!) class library project in C# or VB.NET (this is where you will write your tests)
  • Add a reference to NUnit or MSTest or MbUnit so you can write tests with it.
  • right click on the test project (the standard class library) and Add a reference to the silverlight project that you'd like to test
  • In the test project, remove references to System.core, system.xml
  • in the test project, add references to the silverlight versions of system.core, system.xml and the other system.* dlls from silverlight.
  • Continue changing the references until everthing compiles successfully (If you have resharper it might think that there are problems, but as long as compilation passes you're OK.

Add references for SilverUnit

  • Add references to Typemock.dll (from the ".NET" tab) as well as to the CThru.dll and CThru.Silverlight.dll

You should now be able to write a test that looks somewhat like this:
[TestMethod]
[SilverlightUnitTest]  // <----- where the magic happens
public void SomeTestAgainstSilverlight()
{
//code against silverlight here
}

How to know SilverUnit is working

When SilverUnit is running the first test, you should see in the output console a sentence similar to the following:
"Loading isolation aspects..."
If you see nothing that looks like this before the first test is running it means that something is wrong and SilverUnit is not really isolating you from the Silverlight runtime objects (you'll also get nasty exceptions relating to DependencyObject based classes)

It's not working. What do I do?

  • make sure you have the latest version of Typemock Isolator running and enabled in the process that is running the test. If it's not running inside visual studio you may need to run that process using TMockRunner.exe which is part of Typemock's Installation
  • Make sure you have the latest version of the compiled CThru and SilverUnit binaries (0.4 as of writing). These are compiled against the core typemock.dll with version 5.3. it will not work otherwise.
  • if you are getting "could not load file or dependencies" stuff, it most likely is a reference problem. remove all unneeded references from the test project as long as it still compiles. Also add the silverlight versions of those dlls if they are reported as not found, as references to the test project.

I'm getting a nasty exception from "JoltHelper" and none of the above is relevant

Make sure that you are not creating instances of your controls inside a SetUp or TestInitialize method. Create instances of objects inheriting from DependencyObject only from inside the test, because that is when the SilverUnit engine begins to work. For now this should be the practice. Do not use "new" on things inheriting from DependencyObject in a setup method.
Examples:
Good:
private MyControl control;
[Test,SilverlightUnitTest]
public void SomeTest()
{
control = new MyControl();  //<---- *this is OK*
myControl.DoSomething();//
}


Not good:
private MyControl control;
[SetUp]
public void Setup()
{
control = new MyControl();  //<---- *big no no*
}
[Test,SilverlightUnitTest]
public void SomeTest()
{
myControl.DoSomething();//
}

Can I use SilverUnit inside a project that uses the Silverlight test framework from Microsoft?

You can't. Silverunit is based on the regular CLR runtime, and the MS test framework is compiled against the silverlight runtime. You can't use both of them on the same project (and you shouldn't need to). Have one project which holds unit tests and uses SilverUnit, and another project that holds Integration Tests and uses the MS test framework.

Does SilverUnit support Silverlight 3.0?

Not officially. It has not been tested with it yet. There is a very high chance that tests already written against silverlight 2.0 should just work if the code under test is compiled into Silverlight 3.0.

Last edited May 24, 2009 at 10:38 AM by RoyOsherove, version 16

Comments

martinording Jul 30, 2009 at 3:04 PM 
I created a new silverunit test project using the template, having SL 3.0 as the framework. I right click on ControlTests1.cs and choose Run Test(s). I get:

The target type doesn't contain tests from a known test framework or a 'Main' method.

The same thing happens if I go to the submenu and click Run with Debugger/Coverage/TypeMock. I can run the examples (framework 3.5) from TypeMock just fine. Any ideas?

chakrit May 9, 2009 at 6:56 PM 
TypeMock Isolator is a total deal breaker... is it possible to use another mocking framework instead?