I have a third party component that I am trying to write unit tests around. The problem is that I can't mock the object and there is no interface.
I decided to create an interface and a wrapper class calling into the code for the sake of mocking. I looked into the class definition that is generated by Visual Studio using the meta data and noticed a few things:
- The class has two constructors (one takes a parameter)
- The class inherits from
IDisposable
My questions are:
- Does my implementation below look right?
- Did I handle the
IDisposable
implementation correctly in the proxy class? - Do I need the second constructor in the proxy class since the interface does not support constructor definitions? I use dependency injection in my code and I assume unless I tell my DI framework to use the second constructor I don't really need it but I am not sure.
The meta data looks like (slimmed down version):
public class PopClient : IDisposable
{
public const int DefaultPort = 110;
public const int DefaultSSLPort = 995;
public PopClient();
public PopClient(AddressFamily addressFamily);
public bool HasTimeStamp { get; }
public List<string> Capability();
public void Connect(string host);
protected override void GetServerGreeting();
}
Based on the meta data, my interface looks like (after removing methods/properties/access modifiers that are invalid in an interface definition):
public interface IPopClient : IDisposable
{
bool HasTimeStamp { get; }
List<string> Capability();
void Connect(string host);
void ConnectSSL(string host);
}
Based on the interface, I then created the wrapper class:
public class PopClientProxy : IPopClient
{
private readonly Pop3 pop3;
public PopClientProxy()
this.pop3 = new Pop3();
public PopClientProxy(AddressFamily addressFamily)
this.pop3 = new Pop3(addressFamily);
public bool HasTimeStamp
get { return pop3.HasTimeStamp; }
public List<string> Capability()
return pop3.Capability();
public void Connect(string host)
pop3.Connect(host);
public void Dispose()
{
if (pop3 != null)
pop3.Dispose();
}
}