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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public string GetSourceListFromServer(string source) { // Get the channel from WCF EndpointAddress address = new EndpointAddress("http://y029c.hosts.cx/service.svc"); WSHttpBinding binding = new WSHttpBinding(); ChannelFactory<ISourceListProvider> factory = new ChannelFactory<ISourceListProvider>(binding, address); ISourceListProvider channel = factory.CreateChannel(); // Call the service string result = channel.GetSourceList(source); // Analyze the result if (result == string.Empty) return "Error"; return result; } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
[TestMethod] [Isolated] public void GetSourceListFromServer_SimulateEmptyReturn_ReturnError() { // Fake the Factory object that's created in the method // No need to bother with the parameters. ChannelFactory<ISourceListProvider> fakeFactory = Isolate.Fake.Instance<ChannelFactory<ISourceListProvider>>(); Isolate.Swap.NextInstance<ChannelFactory<ISourceListProvider>>().With(fakeFactory); // Just specify what you want the service to return Isolate.WhenCalled(() => fakeFactory.CreateChannel().GetSourceList("AnyValue")) .WillReturn(string.Empty); // Call the method DataProviderClient client = new DataProviderClient(); string result = client.GetSourceListFromServer("AnyServer"); // Check the expected result Assert.AreEqual("Error", result); } |
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:
- Getting Started with WCF tutorial – Learn to use WCF – tutorial
- WCF Unit Testing – The Client Side – Testing WCF Services, client-side tutorial
- Unit Testing WCF Services tutorial – simulated an asynchronous call to WCF, by setting custom code behavior on the Begin and End.