Unit-Testing Silverlight Projects

In the last post, I wrote about, how to use ReSharper with Silverlight Unit Test Projects. Now I can do TDD with Silverlight very easily. :-)

But I don’t like the MSTest assertions syntax. In the Past I used NUnit to write my tests. How can I use the NUnit syntax inside the Silverlight unit tests?

My first Idea was to write the NUnit assertions syntax by my own to encapsulate the MSTest. But the easiest way is to really use NUnit assertions in MSTest :-)

How does this work?

First of all:

Download NUnit for Silverlight: http://code.google.com/p/nunit-silverlight/ and reference to NUnit.Framework.
(Be sure the assemblies are unblocked. If not, unblock the assemblies in the file properties dialog)

After that:

Write a small alias in the usings of your test class:

using Assert = NUnit.Framework.Assert;

Thats all:

Now you can use the NUnit assertions syntax in your MSTest:

// Assert
Assert.That(result, Is.EqualTo(Visibility.Visible));
Assert.Throws<Exception>(() => hasItemsConverter.Convert(items, null, null, null));

This is not only a solution for Silverlight Unit Test, you can do this with all other MSTest where you want to use NUnit assertions ;-)

Why does that work?

Assertions are only simple tests. If the tests fail, a Exception will be thrown. This is the same procedure in all unit test frameworks. So you can mix the frameworks in your unit test projects.

DotNetKicks-DE Image
MSTest, NUnit, Silverlight, tdd, unit tests , , , ,

TDD with SIlverlight Unit Test Projects

I have searched a while and I have tested many tools and frameworks, but I couldn’t a tool or framework that supports TDD in a right and quick way.

The perfect way is to use the ReSharper Unit Test Runner, which doesn’t supports Silverlight Unit Test Projects.

A view Weeks ago I also tested AgUnit (http://agunit.codeplex.com/) but it doesn’t run with the latest version of Jetbrains ReSharper.

Today I tried AgUnit again, because there is a new version since May 9 2011. Now AgUnit runs really great :-)

All you have to do, is to download AgUnit and extract all the files to ReSharpers “Plugins” folder. Now restart the Visual Studio and create a new Silverlight Unit Test Project and a small test class.

[TestClass]
public class Tests
{
    [TestMethod]
    public void TestMethod1()
    {
        var hallo = "Hallo";
         Assert.AreEqual(hallo, "Hallo");
    }

    [TestMethod]
    public void TestMethod2()
    {
        var hallo = 100;
         Assert.AreEqual(hallo, 100);
    }

     [TestMethod]
    public void TestMethod3()
    {
        var hallo = 100;
         Assert.AreEqual(hallo, 101);
    }
}

Update: Before using AgUnit you have to compile the test project and to ensure, that the project references to the System.Xml.Linq.dll

Now I can start the unit tests using the green ReSharper icons or the unit test runner

testrunner

DotNetKicks-DE Image

AgUnit, ReSharper, Silverlight, tdd, unit tests , , , ,

WP7 App: See# Party (part 1)

“See# Party” (www.seesharpparty.de) is a great .NET community conference in Kreuzlingen (Switzerland) which is organized by the .NET user group of Konstanz-Kreuzlingen (www.dotnetkk.de)

For this second conference, I will write a small Windows Phone 7 App, which allows the users to see all the informations about the speakers, the sessions and the twitter wall. The user (the attendee) has the possibility to create their very own agenda to see which session they want to visit.

In the following blog entries I want to describe the programming of that Windows phone 7 app. This Post starts a serial about creating a WP7 app for the attendees (and other user) of the .NET community conference “See# Party”

The app is hosted at the online repository “BitBucket” because I love Mercurial and all of its features:
https://bitbucket.org/JuergenGutsch/seesharpparty/
Feel free to visit the Repository, take a look at the code, create some Issues or contribute some code.

The main goal is to create a clean code application with strong MVVM in the presentation logic and event based components in the business logic.

The base requirements can be found at BitBucket:
https://bitbucket.org/JuergenGutsch/seesharpparty/issues?status=open

A small note about the name of the conference for all of you who don’t understand German:
“See” is the German word of “Lake” (Kreuzlingen is at the Lake of Constance) and is nearly pronounced like the German pronunciation of the character “C”.
So “C#” and “See#” is nearly pronounced in the same way.

Event Based Components, MVVM, See# Party, Windows Phone 7 , , ,

Publish resources of a CustomControl from an external library in Silverlight

Sometimes it’s useful to add a Style programmatically to a CustomControl.

This part is very easy. You just have to allocate the Style in the Constructor of that Control:

[code lang="csharp"]Style = (Style)Application.Current.Resources["BrowserWindowStyle"];[/code]

This Style is placed in the Current resources of the Application. But what do you do, if you need a default Style which is not in the resources of your application? What do you do, if you don’t want to write the Style in C# directly in the Source of your Control?

You can add a XAML file with a ResourceDictionary to your Library. Maybe called “BrowserWindowStyle.xaml”

You can Load the XAML file as a ResourceDictionary object and add them to the MergeDictionaries of your current Application:

[code lang="csharp"]public BrowserWindow()
{
var dic = ResourceHandler.ResolveResourceDictionaryByPath<BrowserWindow>("Dialogs/BrowserWindowStyle.xaml");
if (!Application.Current.Resources.MergedDictionaries.Contains(dic))
{
Application.Current.Resources.MergedDictionaries.Add(dic);
}

Style = (Style)Application.Current.Resources["BrowserWindowStyle"];
}[/code]

Really simple, if you know how to do that ;-)

The ResourceHandler is a small class which provides useful static methods to resolve resource paths:

[code lang="csharp"]public static ResourceDictionary ResolveResourceDictionaryByPath<T>(string path)
{
var namespacePath = typeof(T).Assembly.ToString().Split(',')[0];

var uri = new Uri(string.Format("/{0};component/{1}", namespacePath, path), UriKind.RelativeOrAbsolute);

return new ResourceDictionary { Source = uri };
}[/code]

DotNetKicks-DE Image
CustomControl, Resources, Silverlight , ,

Hello world!

Welcome to my first Post on Juergens DotNetKicks Blog.
In this blog, I will provide articles about XAML related topics, such as Silverlight, Windows Phone 7, WPF and other XAML
This is my first technology blog, which is complete written in English. I hope you will excuse some typos.
first post, personal , ,