How to mock WCF Web Services with Rhino Mocks.

Posted by Will on Stack Overflow See other posts from Stack Overflow or by Will
Published on 2010-04-15T13:26:46Z Indexed on 2010/04/17 15:43 UTC
Read the original article Hit count: 230

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(); 
    }
}

© Stack Overflow or respective owner

Related posts about rhino-mocks

Related posts about unit-testing