In a previous post I demonstrated testing a Compact Framework application using Passive View. I also mentioned that I would use the same technique to test the presentation layer on other platforms. So to put that to the test I migrated that simple application to Silverlight.

Testing Platform

There are many more testing options for Silverlight than there are for the Compact Framework. If you're using the IDE, you might consider reading this for some of your options.

I chose to test my POCO controller classes inside the regular .Net framework using NUnit.

Instead of retargeting, I cross-compiled the controller class to the 'real' .Net framework. The view controls in Silverlight are a subset of the complete WPF controls, so they can be substituted in the test environment to simulate the runtime controls.

The only slight hindrance I came across was that when you attempt to use the WPF controls in NUnit you get the exception "InvalidOperationException : The calling thread must be STA". The controls insist on running on an STA thread, which requires a .config file for the test assembly containing the following:

        <add key="ApartmentState" value="STA" />

Passive View

As previously, the view is completely dumb. It contains no logic, just the controls that will be manipulated by the controller. The XAML is kept simple with mostly positioning logic.

<StackPanel x:Name="LayoutRoot"

    Demonstration of Passive View to test
        Silverlight client

<Button x:Name="ShowMessage"
    Content="Show Message"
    Width="150"  Margin="20"/>

I exposed the controls as public fields on the View class. These were populated in the 'Loaded' event (note, this only fires in the Silverlight runtime - not the tests), then the controller was wired up:

public class MainView : UserControl

  public Button ShowMessage;
  public TextBlock Message;

  public MainView()
    Loaded +=
      new RoutedEventHandler(UserControl_Loaded);

  private void UserControl_Loaded(object sender,
      RoutedEventArgs e)
    ShowMessage = (Button)FindName("ShowMessage");
    Message = (TextBlock)FindName("Message");
    new MainController(this);

During the tests a testable view class simply populates the fields with 'empty' controls allowing the controller logic to be tested, without requiring a full presentation runtime.

public class TestableView : MainView
  public TestableView()
    ShowMessage = new Button();
    Message = new TextBlock();
    new MainController(this);

Migration from Windows Forms

The only remaining task was to migrate the namespaces to WPF from Windows Forms. Mostly this was just a search/replace exercise (with the compiler telling me everywhere something had changed):

  • Namespace moved to System.Windows;
  • Enabled property is now IsEnabled property;
  • MessageBox.Show has slightly fewer options;
  • Setting colours now uses a Brush class;
  • etc., ...

With just a little more work, you could wrap each control in a common interface (e.g., IButton, IComboBox), then the same controller could be used to run a view on multiple platforms (Silverlight/WPF/Windows Forms). You might even be able to use the controller to run a web application using a framework like Visual WebGui (which makes coding a web application magically like coding a Windows Forms application).

For a larger application I would definitely do this extra work - it is bound to pay itself back at some point, not least of all because you could have stubbed control implementations in the tests instead of the 'real' ones.


The modifications to move the demo from Windows Forms to Silverlight were a rewriting of the View (inevitable), and some minor syntax changes in the controller.

On a larger application the ability to keep 90% of your code (and the tests!) when migrating to another platform is invaluable, and Passive View is the best way I've seen of writing (and testing) the presentation layer.

Silverlight Passive View Demo
download, unzip, run CommandPrompt.bat, and type 'NAnt'

View Silverlight Passive View Demo Online

Submit this story to DotNetKicks Shout it