---
title: "Unit Testing in silverlight land with Typemock Isolator"
url: "https://osherove.com/blog/2008/12/27/unit-testing-in-silverlight-land-with-typemock-isolator.md"
canonical: "https://osherove.com/blog/2008/12/27/unit-testing-in-silverlight-land-with-typemock-isolator.html"
published: "2008-12-27T14:54:07"
updated: "2008-12-27T14:54:07"
tags: ["Art Of Unit Testing", "Silverlight", "Typemock", "Unit Testing"]
author: "Roy Osherove"
description: "I’ve been asked quite a lot recently whether one can write unit tests and isolate logical code that runs inside a silverlight application. Up until today my initial answer was ‘no’ because silverlight runs under…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# Unit Testing in silverlight land with Typemock Isolator

By Roy Osherove December 27, 2008 In [Art Of Unit Testing](https://osherove.com/blog/category/art-of-unit-testing), [Silverlight](https://osherove.com/blog/category/silverlight), [Typemock](https://osherove.com/blog/category/typemock), [Unit Testing](https://osherove.com/blog/category/unit-testing)

**Historical article** · Originally published December 27, 2008; technical details may be outdated.

I’ve been asked quite a lot recently whether one can write unit tests and isolate logical code that runs inside a silverlight application. Up until today my initial answer was  ‘no’ because silverlight runs under different versions of mscorlib.dll.

however.

Today I actually gave it a try and realized that writing unit tests (not [integration tests, as the silverlight test framework](http://www.jeff.wilcox.name/2008/03/silverlight2-unit-testing/) allows) against silverlight based code is possible and quite easy. Just like any other code that relies on a third party platform (like sharepoint code) the silverlight related code might have various dependencies.

I’m going to show how to use [Typemock Isolator](http://www.typemock.com/) to overcome a couple of simple silverlight dependencies (using HtmlPage) and how to setup a test project against a silverlight project (with NUnit or MsTest)

**Assumptions:**

1. You have an open solution
2. the solution contains a silverlight class library or silverlight application project

**To setup a test project against silverlight using MSTest:**

1. Add a new test project to the solution
2. Remove all the existing classes (Database test, ordered test etc..) so that you are only left with the unit test class (UnitTest1).
3. Remove all useless comments and crud code from the test class so that you are only left with a test method (no comments, not even the TestContext)
4. Add a reference to the silverlight versions of “System.dll”, “System.Windows.dll” to the test project. (usually located under “C:\\Program Files\\Microsoft SDKs\\Silverlight\\v2.0\\Reference Assemblies\\” (remove existing reference to system.dll if you need to first)
5. Add a reference to the project under test
6. You can now write tests against the object model (standard classes)

**To setup a test project against silverlight using NUnit or MbUnit:**

1. Add a new class library to the solution as your test project
2. Add a reference to the silverlight versions of “System.dll”, “System.Windows.dll” to the test project. (usually located under “C:\\Program Files\\Microsoft SDKs\\Silverlight\\v2.0\\Reference Assemblies\\” (remove existing reference to system.dll if you need to first)
3. Add a reference to the project under test
4. You can now write tests against the object model (standard classes)

**How to break the dependencies**

Now, here is a simple example of code that has a silverlight dependency we’d like to test:

Let’s say we have a class in the silverlight project called ChatSession (I’m basing this on ScottGu’s Chat demo). but it’s constructor looks like this:

![WindowClipping](https://osherove.com/img/wb/a1766881_weblogs.asp.net_blogs_rosherove_WindowClipping_thumb_qfKb_Q.png)

What if we wanted to control during our test whether the page is enabled or disabled?

Here’s one way to do it using Isolator:

1. In your test project add two references under the .NET tab: “Typemock Isolator” and “Typemock ArrangeActAssert”
2. Write the following test:

![WindowClipping (2)](https://osherove.com/img/wb/03cf399c_weblogs.asp.net_blogs_rosherove_WindowClipping_20_2__thumb_4mlE_A.png)

Using Isolate.WhenCalled() we are able to circumvent any method (static or not) to return whatever we want, or throw an exception.

Here’s a more interesting case. Let’s say we have a method that modifies the current page and shows some html to the user in a span tag:

![WindowClipping (3)](https://osherove.com/img/wb/fa3e7bd7_weblogs.asp.net_blogs_rosherove_WindowClipping_20_3__thumb_Bf9WqQ.png)

here is one way to write a test that makes sure that the right text is set into the message element in the page, without needing to have a real page present:

![WindowClipping (4)](https://osherove.com/img/wb/670dfa24_weblogs.asp.net_blogs_rosherove_WindowClipping_20_4__thumb_elBN9g.png)

There are several things to note here:

1. HtmlElement is **Sealed** has an **internal constructor**, but we can still create a fake instance of it using Isolator. None of the other frameworks can do this.
2. We are returning a fake object from a **method call chain** (HtmlPage.Document.GetElementById ) without needing to create a separate stub for **document**. None of the other frameworks can do this (especially since it is based on a **static** method call to begin with)
3. We assert in the end that the method was indeed called with the correct arguments without needing to change a single piece of code. Granted, I wouldn’t write code like this (I like decoupling!) but sometimes you just don’t have the ability to change existing code.

If you are developing an open source silverlight project, it is important to note that there is a free full version of isolator for open source projects with full functionality.

These are just the beginning of my journey into silverlight unit testing. I’m looking for good code examples that you might want to test, with various dependencies that need breaking. your comments are appreciated.

## Lessons on this topic

- [Code Excavations and Domain Specific Test Frameworks](https://courses.osherove.com/courses/the-art-of-unit-testing-and-tdd-master-class-in-net/code-excavations-and-domain-specific-test-frameworks/) · The Art of Unit Testing & TDD Master Class in .NET

## More in Art Of Unit Testing

[All posts in Art Of Unit Testing](https://osherove.com/blog/category/art-of-unit-testing)

- Mar 17, 2009

    ### [API design values and decision matrix](https://osherove.com/blog/2009/3/17/api-design-values-and-decision-matrix.html)

- Mar 16, 2009

    ### [Art of unit Testing goes to print in April](https://osherove.com/blog/2009/3/16/art-of-unit-testing-goes-to-print-in-april.html)

- Nov 2, 2008

    ### [Isolator Feature Focus: Duck Typing and Isolate.Swap](https://osherove.com/blog/2008/11/2/isolator-feature-focus-duck-typing-and-isolateswap.html)

- Oct 11, 2008

    ### [Art Of Unit Testing – Book done, Wiki ready](https://osherove.com/blog/2008/10/11/art-of-unit-testing-book-done-wiki-ready.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
