Testing WCF with Isolator – The Client

In my last post about testing WCF, I described how to easily test a server side component which is exposed by WCF. And obviously, the next step is to go to the client side. For this to work you don’t need configuration files, or any other definition. Isolator just needs type definition.

So here’s a consumer of my service:

The client, at construction, connects to the service, and at the GetSourceListServer, calls it, and performs a simple logic to see if the result has any meaning.

Note the code is refactored a bit. I could go on and fake all the nasty little WCF objects, but by extracting them into a single method, I just fake this method, making my life easier, and my tests more readable, and less fragile.

Let’s start with a simple test – fake the channel, and verify that the correct argument was sent to the server:

And the SetupFakeChannelFactory private method looks like that:

The SwapNextInstance replaces the Swap method (based on customer feedback, who found this clearer), but does exactly the same: It sets up the fakeFactory to be swapped with the next instance of this type that will be created (this will happen inside the client’s constructor). The next line sets a return value as the provider. Note that our provider is not a concrete type – it’s a fake interface, representing our service.

I can of course test what happens when an incorrect result is returned from the server (I expect an exception in this case):

But if I want to test that something occurred on the wire, I can inject a CommunicationException (which causes an “Error” to be returned):

So there you go, WCF simply isolated. Now you can test your logic around it, no excuses.