23 December, 2008

WCF: How to unit test a WCF-service

Unit testing a WCF-service is actually not all that difficult as one might think initially.

The better way of unit testing a WCF-service, is to actually host both the ServiceHost as well as the Client within the unit test at the same time. In this way – you are completely liberated from dependencies and other constraints otherwise imposed.

The Microsoft unit testing framework included in VS (2005/2008/2010) contains a number of attributes that you can use for setting up the servicehost. This is the way of setting up a unit test for testing a WCF-service:

1. Declare a static class member for the ServiceHost:

2. Initialize and open the ServiceHost in the ClassInitialize method of the unittest. This is only run once when the unit test is started.

3. Close the ServiceHost in the ClassCleanup method of unit test.

4. Use the ServiceHost inside all unit test methods as an ordinary WCF-service host. The host will respond as expected and you do not have to start a console behind the unit test to get valid replies from the service.

Of-course you have to setup the app.config for your unit test to make the ServiceHost run as expected (note the ‘local’ endpoint which is referenced in the unit testing). But this is rather conventional WCF-settings.

This is a really nice and clean way of performing unittesting of WCF-services.

A sample (POC) is found here: http://www.clauskonrad.net/download.ashx?id=11

Technorati Tags: ,

07 December, 2008

NMock Not Executed (not trusted location)

I have begun reading the book “Microsoft .NET: Architecting Applications for the Enterprise” by Dino Esposito. One of his many chapters touches on the matter of Unit testing and usage of Mocks for factoring out dependencies of classes.

So I downloaded NMock2 from sourceforge and extracted it to disk. The very first thing I was presented with when running a newly created Unittest where I used the NMock framework was the below (Test not Executed):


If you press the hyperlink “Test run error” you see this message: “The location of the file or directory xxx is not trusted


What to do?
Well – it is quite simple to mitigate this situation (to make the NMock assembly a trusted companion). Just find the NMock assembly on disk and “unblock” it! You are good to go. It does however require a restart of Visual Studio to make the latter comprehend that you consider the NMock assembly a trusted companion.


Link to sourceforge: http://sourceforge.net/projects/nmock2

iPhone/XCode - not all cases are equal!

This bit me! Having made some changes to an iPhone application (Obj-C); everything worked fine in the simulator. But, when deploying the s...