views:

55

answers:

2

How do I test a class that utilizes proxy clients generated by a Web Service Reference?

I would like to mock the client, but the generated client interface doesn't contain the close method, which is required to properly terminate the proxy. If I don't use the interface, but instead a concrete reference, I get access to the close method but loose the ability to mock the proxy.

I'm trying to test a class similar to this:

public class ServiceAdapter : IServiceAdapter, IDisposable
{
    // ILoggingServiceClient is generated via a Web Service reference
    private readonly ILoggingServiceClient _loggingServiceClient; 

    public ServiceAdapter() : this(new LoggingServiceClient()) {}

    internal ServiceAdapter(ILoggingServiceClient loggingServiceClient)
    {
        _loggingServiceClient = loggingServiceClient;
    }


    public void LogSomething(string msg)
    {
        _loggingServiceClient.LogSomething(msg);
    }

    public void Dispose()
    {
        // this doesn't compile, because ILoggingServiceClient doesn't contain Close(), 
        // yet Close is required to properly terminate the WCF client
        _loggingServiceClient.Close(); 
    }
}
+1  A: 

I'd create another interface that inherits from your ILoggingServiceClient but adds the Close method. Then create a wrapper class that wraps the LoggingServiceClient instance. Something like:

public interface IDisposableLoggingServiceClient : ILoggingServiceClient
{
    void Close();
}

public class LoggingServiceClientWrapper : IDisposableLoggingServiceClient
{
    private readonly LoggingServiceClient client;

    public LoggingServiceClientWrapper(LoggingServiceClient client)
    {
        this.client = client;
    }

    public void LogSomething(string msg)
    {
        client.LogSomething(msg);
    }

    public void Close()
    {
        client.Close();
    }
}

Now your service adapter can use IDisposableLoggingServiceClient.

Patrick Steele
A: 

In my experience the time required to test something like this isn't really worth it. Properly done your adapter should mostly just be doing a pass through since it's main purpose is to provide a test seam for calling code. At that point it's kind of like properties and views. You don't need to test them because you can visually inspect the code and it's so simple you know it's right.

ShaneC