Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / DevOps / unit-testing

Generate interface for WebService client stub by using CodeDom

0.00/5 (No votes)
8 Apr 2013CPOL 79.9K   102  
Generate interface for WebService client stub for unit testing.

Introduction

This is a simple application that generate the interface for the WebService client stub.

Background

What the generated interface can do ? Em... Because the all methods of the WebService client stub are not virtual. So, it is hard to apply unit testing on when business logic depends on it (the method cannot be mocked).

Using the code

It is very simple that using this function, like this:

C#
var basePath = @"D:\Workspace\YourSolution\YourProject";  // The dir contains the .csproj file.
var webReference = "WebReference";                        // The namespace of the web reference you set.
var puller = new InterfacePuller(basePath, webReference);
puller.Process();    

A code file named "ReferenceExtensions.cs" will be generated under the path <basePath>/Web References/<webReference>.

Now, you can declare the fields with the interface type instead of the client stub class type.

C#
private WebService service = new WebService(); 
private IWebService service = new WebService();  

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)