RE: How do I unit test a Dispose method ?
Gishu Pillai <[email protected]> Mon, 10 Feb 2014 15:24:21 +0530
| Newsgroups | gmane.comp.programming.test-driven-development |
|---|---|
| Message-ID | <CABT02ts=DBsBk4cJ7VmCGHabFakKjathPduu9+LLOgbcNjG+=A@mail.gmail.com> |
Nope. I did test-drive this design.
The connection instance represents an open connection to a hardware device.
All conn parameters appear as constructor parameters of this type - one of
which is a SecureString password.
Connection(conn params)
The type creates a copy of the same in the ctor and holds on to it for
subsequent API calls to the device
Finally it has a Disconnect method that closes the connection.
My problem is detecting whether disconnect disposed of the 'disposable'
member fields.
Subclassing and overriding Dispose is an option
override void Dispose()
{
base.Dispose();
// check if this.Password has been disposed
}
However there isn't any clean way to know this - unless IDisposable
interface had a bool IsDisposed property.
A crude way would be to invoke another member and expect an
ObjectDisposedException (if the type has been well written)