¿ªÔÆÌåÓý

ctrl + shift + ? for shortcuts
© 2025 Groups.io

Re: [TDD] How do I unit test a Dispose method ?


 

Isn't there already an IDisposable interface? which means you can easily create an IDisposable MOCK object and check if dispose was called on it.
all you would do it then have casted IDisposable in the class level that you would access from the dispose method.

public Connection(SecureString password)
{
IDisposable? _myDisposablePassword;
? _myPassword = password.Copy(); // creates a clone of the password which needs to be disposed
_myDisposablePassword = _myPassword;
}
public void Dispose()
{
? _myDisposablePassword.Dispose(); // releases any resources it holds
}


On Thu, Jan 23, 2014 at 12:06 PM, Gishu Pillai <gishu.pillai@...> wrote:
?

public Connection(SecureString password)
{
? _myPassword = password.Copy(); // creates a clone of the password which needs to be disposed
}
public void Dispose()
{
? _myPassword.Dispose(); // releases any resources it holds
}

SecureString is a .Net framework type. I could wrap it in an adapter to ease testing...but doesn't solve the general problem. Wrapping all member disposable type with interfaces is going to be tedious.

It seems this is a better fit for Static code analysis.

Gishu





--
Thanks,

Roy Osherove

?? -
? ?- Read my new book
? ?- My blog for team leaders:
? ?- +47-96-90-22-15 (Oslo Time)


Join [email protected] to automatically receive all group messages.