In this post I’ll explain the steps to generate unit tests for a project which calls a WCF service using Fakes. Microsoft Fakes is an isolation framework for creating delegate-based test stubs and shims in .NET Framework applications. The Fakes framework can be used to shim any .NET method, including non-virtual and static methods in sealed types.
The Fakes framework helps developers create, maintain, and inject dummy implementations in their unit tests. The Fakes framework generates and maintains stub types and shim types for a system under test.
I had previously discussed about creating Unit Tests using Pex, Moles and Visual Studio 2010. The projects inside the sample solution are
The solution structure is displayed below
DemoLibrary calls DemoService though proxy as displayed in the Layer diagram
I’ll now discuss in brief the code snippets of each project
WCF Service(DemoService): This service provides only a single operation
[ServiceContract]
public interface IDemoService
{
[OperationContract]
string Search(string criteria);
}
WCF Service Client(DemoLibrary): It calls the Search method of DemoService through proxy as displayed below
public string GetResults(string s)
DemoServiceReference.DemoServiceClient client = null;
try
client = new DemoServiceReference.DemoServiceClient();
client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
client.ChannelFactory.Credentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;
s = client.Search(s);
return s;
finally
if (client != null)
if (client.State == CommunicationState.Opened)
client.Close();
else if (client.State == CommunicationState.Faulted)
client.Abort();
Adding Unit Tests for DemoLibrary:
In order to Unit Test WCF Service Client(DemoLibrary) project using Fakes and Visual Studio 11 the steps are
[TestMethod]
public void TestSearch()
using (ShimsContext.Create())
ShimWCFService<IDemoService>();
ShimDemoServiceClient.Constructor = (var1) =>
new ShimDemoServiceClient { };
};
ShimDemoServiceClient.AllInstances.SearchString = (var1, var2) =>
return "Result";
Search search = new Search();
string result = search.GetResults("test");
Assert.IsNotNull(result);
Assert.AreEqual(result, "Result");
/// <summary>
/// Mocks the WCF service.
/// </summary>
private void ShimWCFService<TService>() where TService : class
ShimClientCredentials.Constructor = (var1) =>
new ShimClientCredentials();
ShimClientCredentials.AllInstances.WindowsGet = (var1) =>
return new ShimWindowsClientCredential();
ShimWindowsClientCredential.AllInstances.ClientCredentialGet = (var1) =>
return new System.Net.NetworkCredential();
ShimWindowsClientCredential.AllInstances.ClientCredentialSetNetworkCredential = (var1, var2) => { };
ShimWindowsClientCredential.AllInstances.AllowNtlmGet = (var1) => { return true; };
ShimWindowsClientCredential.AllInstances.AllowNtlmSetBoolean = (var1, var2) => { };
ShimWindowsClientCredential.AllInstances.AllowedImpersonationLevelGet = (var1) => { return System.Security.Principal.TokenImpersonationLevel.Impersonation; };
ShimWindowsClientCredential.AllInstances.AllowedImpersonationLevelSetTokenImpersonationLevel = (var1, var2) => { };
ShimChannelFactory.AllInstances.CredentialsGet = (var1) =>
{ return new ShimClientCredentials(); };
ShimClientBase<TService>.AllInstances.ClientCredentialsGet = (var1) =>
return new System.ServiceModel.Description.ClientCredentials();
ShimClientBase<TService>.AllInstances.ChannelFactoryGet = (var1) =>
return new ShimChannelFactory<TService>();
ShimClientBase<TService>.AllInstances.StateGet = (var1) =>
return CommunicationState.Opened;
ShimClientBase<TService>.AllInstances.Close = (var1) =>
{ };
ShimClientBase<TService>.AllInstances.Abort = (var1) =>
The sample is available for download @ Download Source code