How to Use Pex and Moles to Generate Unit Tests for a Project Having External Dependency (WCF Proxy) using Visual Studio 2010 SP1

In this post, I’ll discuss about writing unit tests using Pex and Moles. Pex and Moles are Visual Studio 2010 Power Tools that help Unit Testing .NET applications.

Pex automatically generates test suites with high code coverage. Right from the Visual Studio code editor, Pex finds interesting input-output values of your methods, which you can save as a small test suite with high code coverage.

Moles allows to replace any .NET method with a delegate. Moles support unit testing by providing isolation by way of detours and stubs, i.e., Generate a Mole type from the original type and redefine its behavior using delegates.

I’ll explain the steps to generate unit tests for a project which calls a WCF service. Pex will be used to generate unit tests. Moles will be generated to isolate the external dependency (WCF proxy) and behavior will be redefined using delegates.

The projects inside the sample solution are:

DemoService: This project is a WCF Service.

DemoLibrary: This project is a Class library and service reference to DemoService has been added. Unit tests will be generated for this project.

ConsoleApp: This project is a Console application.

DemoLibrary.Tests: This is a Test project and contains unit tests for DemoLibrary.

There is an external dependency (GetResults makes a service call though the WCF Proxy) so “Run Pex Explorations” will not generate unit tests as displayed below:

In order to isolate the external dependency, we need to generate Moles before running Pex Explorations. Moles will be generated for DemoLibrary and System.ServiceModel assemblies and behavior will be redefined using delegates. There are two ways to generate a mole for an assembly. I’ll show you both the approaches:

Visual Studio: This is the easiest way.

Right click on the reference and generate Moles for that assembly as displayed below:

A .Moles file will be added to the project. Build the project and Moles.dll will be added to MolesAssemblies folder as displayed below:

Command Prompt: Moles can be generated from the command prompt.

Run the moles.exe and specify the assembly path for which Moles needs to be created.

Copy the generated assembly to the Project and add reference to it.

Similarly, as explained above, we need to generate Moles for System.ServiceModel assembly. For Visual Studio 2010 SP1, this may fail with error message “The type or namespace name 'IHttpCookieContainerManager' does not exist in the namespace 'ssm::System.ServiceModel.Channels' (are you missing an assembly reference?)”. This step however works fine for Visual Studio 2010. The fix is to exclude the type, i.e., 'IHttpCookieContainerManager' from StubGeneration as displayed below:

The next step is to Mock the Service call (redefine behavior using delegates) and add Asserts as displayed in code snippets below:

Run Pex Explorations to generate unit tests for GetResults method as displayed below:

Unit tests will be added to the Test Class as displayed below:

Go to Test View and Run the unit tests as displayed below:

Summary

In a similar way, we can extract out external dependencies using Moles and then run Pex Explorations. A few examples of external dependencies can be data layer, UI layer, server calls, etc. You can read more about Pex and Moles at http://research.microsoft.com/en-us/projects/pex/.