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();
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With