views:

118

answers:

2

I have a C# class that gets generated using the wsdl.exe tool that looks something like this:

public partial class SoapApi : System.Web.Services.Protocols.SoapHttpClientProtocol
{
    public SOAPTypeEnum AskServerQuestion()
    {
        object[] results = return this.Invoke("AskServerQuestion");
        return (SOAPTypeEnum) results[0];
    }
}

I have some thin wrapper code around this that keeps track of the result, etc. Is it possible to use any of the object mocking frameworks to make a fake SoapApi class and return predictable results for each of the calls to the thin wrapper functions?

I can't make the AskServerQuestion() function virtual because it's auto-generated by the wsdl.exe tool.

A: 

The class is partial so you could make the class implement an interface in the partial class part you write. You can then mock the interface.

DW
+2  A: 

The way I've accomplished this was to inject an ISoapApi instead, where the ISoapApi interface mimics the automatically generated SOAP API.

For your case:

public interface ISoapApi
{
    SOAPTypeEnum AskServerQuestion ();
}

Then, take advantage of the fact that the generated SoapApi class is partial, and add this in another file:

public partial class SoapApi : ISoapApi
{
}

Then, consumers should just take an ISoapApi dependency that can be mocked by any of the mocking frameworks.

One downside is, of course, when the SOAP api changes, you need to update your interface definition as well.

Pete
Thanks a lot, worked like a charm. I had to add some stuff because the generated class implemented another interface, but that was definitely the correct path to go down.
ryancerium
I wish that the wsdl tools for C# would automatically generate a service interface when *consuming* web services, to save us the trouble, but I've been unable to find any way to make it do that, sadly.
Pete