WCF actually cries out for unit testing. How can you make sure your logic actually works without relying on a response from a server? What happens when something happens on the wire? All kind of happenings make your tests not-repeatable.
And how is your logic handling what’s happening on the wire? A timeout for example. How do you test that? And finally, on the server side, how do you simulate issues that should come from the client or the wire?
You already know the answer.
Our example is simple. We have a single method we’re calling on the server side. Let’s take a look at the server code:
1 2 3 4 5 6 7 |
<span style="color: #000000;">[ServiceContract(Namespace=<span class="str">"http://AnyServer"</span>)]</span> <span style="color: #000000;"><span class="kwrd">public</span> <span class="kwrd">interface</span> ISourceListProvider</span> <span style="color: #000000;">{</span> <span style="color: #000000;"> [OperationContract]</span> <span style="color: #000000;"> <span class="kwrd">string</span> GetSourceList(<span class="kwrd">string</span> source);</span> <span style="color: #000000;">}</span> |
and here’s the server code of our XMLProvider:
1 2 3 4 5 6 7 8 9 |
<span style="color: #000000;"><span class="kwrd">public</span> <span class="kwrd">string</span> GetSourceList(<span class="kwrd">string</span> source)</span> <span style="color: #000000;">{</span> <span style="color: #000000;"> MessageHeaderInfo headerInfo = OperationContext.Current.IncomingMessageHeaders[0];</span> <span style="color: #000000;"> <span class="kwrd">if</span> (headerInfo.Name != <span class="str">"ID"</span>)</span> <span style="color: #000000;"> {</span> <span style="color: #000000;"> <span class="kwrd">throw</span> <span class="kwrd">new</span> ArgumentException(<span class="str">"Bad Header Info"</span>);</span> <span style="color: #000000;"> }</span> <span style="color: #000000;"> <span class="kwrd">return</span> <span class="str">"<SourceList><Source value="</span>+source+<span class="str">"/></SourceList>"</span>;</span> <span style="color: #000000;">}</span> |
I would like to test this method, based on it’s header. In this case I want to check the Name of the header info. With Isolator, here’s a test:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<span style="color: #000000;">[TestMethod]</span> <span style="color: #000000;">[Isolated]</span> <span style="color: #000000;"><span class="kwrd">public</span> <span class="kwrd">void</span> GetData_SimulateGoodMessageHeader()</span> <span style="color: #000000;">{</span> <span style="color: #000000;"> Isolate.Fake.StaticMethods<OperationContext>();</span> <span style="color: #000000;"> MessageHeaderInfo fakeInfo = Isolate.Fake.Instance<MessageHeaderInfo>();</span> <span style="color: #000000;"> Isolate.WhenCalled(() => fakeInfo.Name).WillReturn(<span class="str">"ID"</span>);</span> <span style="color: #000000;"> Isolate.WhenCalled(() => OperationContext.Current.IncomingMessageHeaders[0]).WillReturn(fakeInfo);</span> <span style="color: #000000;"> XMLProvider provider = <span class="kwrd">new</span> XMLProvider();</span> <span style="color: #000000;"> <span class="kwrd">string</span> result = provider.GetSourceList(<span class="str">"MySource"</span>);</span> <span style="color: #000000;"> Assert.AreEqual(<span class="str">@"<SourceList><Source value=MySource/></SourceList>"</span>, result);</span> <span style="color: #000000;">}</span> |
This is a simple test, with a simple setup – we fake the static calls (which will be out shortly in version 5.1), fake a MessageHeaderInfo object, and inject a name to satisfy the logic and confirm the result.
Now let’s see what happens if we get a bad message header. This time we expect an exception to occur:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<span style="color: #000000;">[TestMethod]</span> <span style="color: #000000;">[ExpectedException(<span class="kwrd">typeof</span>(ArgumentException))]</span> <span style="color: #000000;">[Isolated]</span> <span style="color: #000000;"><span class="kwrd">public</span> <span class="kwrd">void</span> GetData_SimulateBadMessageHeader()</span> <span style="color: #000000;">{</span> <span style="color: #000000;"> Isolate.Fake.StaticMethods<OperationContext>();</span> <span style="color: #000000;"> MessageHeaderInfo fakeInfo = Isolate.Fake.Instance<MessageHeaderInfo>();</span> <span style="color: #000000;"> Isolate.WhenCalled(() => fakeInfo.Name).WillReturn(<span class="str">"Error"</span>);</span> <span style="color: #000000;"> Isolate.WhenCalled(() => OperationContext.Current.IncomingMessageHeaders[0]).WillReturn(fakeInfo);</span> <span style="color: #000000;"> WCFServer.XMLProvider provider = <span class="kwrd">new</span> WCFServer.XMLProvider();</span> <span style="color: #000000;"> <span class="kwrd">string</span> result = provider.GetSourceList(<span class="str">"MySource"</span>);</span> <span style="color: #000000;">}</span> |
Simple, isn’t it? And this is on the server side. Next time, we’ll look at how we unit test the client side.
Want to start unit testing? Take advantage of our 30-day FREE trial of Isolator Complete. Get it now.