Re: How do I unit test a Dispose method ?

Roy Osherove <[email protected]> Thu, 23 Jan 2014 13:17:35 +0100
Newsgroups gmane.comp.programming.test-driven-development
Message-ID <CAAMhxnzK2no3W34g54OA=wvz5VcLy9JnZHthE4xy8e2GwH_rZA@mail.gmail.com>
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 <[email protected]>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

   - *@RoyOsherove* <https://twitter.com/RoyOsherove>
   - Read my new book *Notes to a Software Team Leader
<http://TeamLeadSkills.com>*
   - My blog for team leaders: *http://5Whys.com* <http://5Whys.com>
   - +47-96-90-22-15 (Oslo Time)