chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male

Unit Testing WCF – Server and Client

Enjoy all benefits of the powerful framework while still performing easy code testing

WCF (Windows Communication Foundation) gives the user a unified framework for easily building service-oriented applications. WCF services help you to create distributed systems using a service-oriented architecture that has flexibility and security. It is widely used for providing and accessing remote services and is a powerful framework that lets you develop your logic without dealing with the infrastructure. However, despite its popularity unit testing the framework’s logic is extremely complex.

What is the problem with unit testing WCF?

For testing code that uses WCF, we need to set up the service to respond correctly for each test. However, tests run for a long time as they make calls on the wire. In addition, what if the network is down? Does that mean you can’t run the tests? Typemock Isolator has come along to make it easy to test code that is dependent on WCF services, without calling the services themselves and without changing it for testing.

How does Isolator solve the WCF unit testing problem?

Unit testing WCF code requires isolation from the framework. With Typemock Isolator you can write unit tests for your logic, without changing ANY of the code. Typemock Isolator helps simulate the services so your tests run locally and quickly while giving you the confidence that your code works correctly… even when the network is down!

Let’s look at an example. We have a service, IServiceListProvider, which we want to call and we also would like to retrieve a string containing a list of our favorite sources. Our client looks like this:

The first lines call the boiler-plate code for accessing the WCF platform to get the channel. Our client then calls the services and analyzes its result. The question is how can we test that GetSourceListFromServerreturns “Error” in case the service returned an empty string?

With Typemock Isolator, we’ll fake the WCF code, and make sure no external calls are made. Then we’ll simulate the service call to return an empty string to make sure our method returns the value “Error”.

Here’s how a test looks like with Isolator:

We faked the necessary WCF objects (the rest are either unnecessary or faked implicitly). We then specify what’s important for the test – the empty string to be returned – and just call the tested method. We’ve easily simulated the WCF setup and calls, ending up with a concise, quick test. That’s what easy unit testing all about.

Download a free trial of Typemock’s WCF Unit Testing tool to start writing your own WCF tests or click through our resources below for all the information and support you need to effectively unit test in WCF. 

Tutorials: Unit Testing WCF

How to use Isolator to test WCF: