¿ªÔÆÌåÓý

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

Testing a class which utilizes randomness

Nikola Novak
 

Hello!

I'm new to TDD and this group. For practice, I started a simple project and now I want to create a list of random strings, but such that no two strings are ever the same. In a very unlikely, yet possible case, when two randomly generated strings are the same, the method that does this should discard the duplicate and generate a new string before it returns.

What I want to test is exactly this behavior - the code which discards the duplicate and forces the string to be regenerated. To test that, I need to force the random string generator to return a duplicate at least once (but finite number of times). To create the test which would generate strings until there's at least one duplicate is unrealistic, because such a test could take a very long time to complete.

How would I write such a test?

Kind regards,
Nikola


Re: How do I unit test a Dispose method ?

 

Whoops just saw yesterday's message came in all garbled..

here it is again properly formatted..


==========================


Hi again Gishu and all,?


Seems like this thread has grown since I last looked :)


What I used to do in this case is inject the disposable object as a collaborator (interface representing its role) into the class that uses it,?


so the class that uses it doesn't need to know about if it is or isn't disposable.


E.g:?


class connection?

{

?ISecurePassword pswd?

}


interface ISecurePassword?

{?

?Void doSomethingThatSecurePasswordsDo();?

}


class DispisableSecurePassword : ISecurePassword, IDisposable

{

? ...?

}


Application:

void Main(){

? using (DisposableSecurePassword pswd = new DisposableSecurePassword())

? {

? ? Connection conn = new connection(pswd):

? ? conn.DoSomethingThatNeedsToBeDone();

? }

}


Makes any sense??


Written hastily from iPhone waiting for a buss


Avi


Re: How do I unit test a Dispose method ?

 

Hi again Gishu and all, Seems like this thread has grown since I last looked :) What I used to do in this case is inject the disposable object as a collaborator (interface representing its role) into the class that uses it, so the class that uses it doesn't need to know about if it is or isn't disposable. E.g: Class connection { ISecurePassword pswd } Interface ISecurePassword { Void doSomethingThatSecurePasswordsDo(); } Class dispisableSecurePassword : ISecurePassword, IDisposable{ ... } Application: Using (disposableSecurePassword pswd = new disposableSecurePassword()){ Connection conn = new connection(pswd): ... } Makes any sense? Written hastily from iPhone waiting for a buss Avi


Re: How do I unit test a Dispose method ?

 

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)


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

Donaldson, John
 

¿ªÔÆÌåÓý

Yes, +1 to that.

?

I¡¯ve been wondering what the business or functional need was that was driving that particular implementation and whether it might have been done differently.

And for sure it would work out differently if you used a language that didn¡¯t ¡°dispose¡±.

?

John D.

?

From: testdrivendevelopment@... [mailto:testdrivendevelopment@...] On Behalf Of avi_a@...
Sent: 05 February 2014 08:58
To: testdrivendevelopment@...
Subject: [TDD] RE: How do I unit test a Dispose method ?

?




Hi Gishu

?

Sorry if my reply is out of context - I've been having trouble finding all the replies in the new (?) Yahoo group UI....

?

In general - the idea is the tests drive your design.

Here it sounds like you have a predefined design (a Connection wrapping your disposable SecireString member) and now you want to test iot (after the code is written).

Did I understand that correctly?

?

Either way - TDDing this would probably produce a whole different design, depending on your requirements... I'd love to give it a go - could you give some more context? as to how you ended up with this design and what the requirements are?

?

Best,

Avi

?





Re: How do I unit test a Dispose method ?

 

Hi Gishu


Sorry if my reply is out of context - I've been having trouble finding all the replies in the new (?) Yahoo group UI....


In general - the idea is the tests drive your design.

Here it sounds like you have a predefined design (a Connection wrapping your disposable SecireString member) and now you want to test iot (after the code is written).

Did I understand that correctly?


Either way - TDDing this would probably produce a whole different design, depending on your requirements... I'd love to give it a go - could you give some more context? as to how you ended up with this design and what the requirements are?


Best,

Avi



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

 

¿ªÔÆÌåÓý

AHA!

But then a factory should not be using the objects it creates¡­. J

?

From: testdrivendevelopment@... [mailto:testdrivendevelopment@...] On Behalf Of Avi Kessner
Sent: Monday, February 03, 2014 8:40 AM
To: testdrivendevelopment@...
Subject: RE: [TDD] How do I unit test a Dispose method ?

?

?

Unless it's a factory

On Feb 3, 2014 6:34 PM, "Amir Kolsky" <amir.kolsky@...> wrote:

?

I agree, my point is that you should ALWAYS be given the object rather than it belonging to you. Therefore, the whole IDisposable pattern seems flawed from a design perspective.

?

All that said, you can still test drive a flawed design¡­. J

?

From: testdrivendevelopment@... [mailto:testdrivendevelopment@...] On Behalf Of Brad Wilson
Sent: Monday, February 3, 2014 7:48 AM
To: testdrivendevelopment@...
Subject: Re: [TDD] How do I unit test a Dispose method ?

?

?

...except, if you're given the object rather than creating it yourself, then its lifetime doesn't belong to you, and you don't need to be concerned with whether it's disposable.

?

On Sun, Feb 2, 2014 at 11:36 PM, Amir Kolsky <amir.kolsky@...> wrote:

?

If A cares about this specific property it would require B to implement IDisposable.

From A¡¯s perspective, it is given an object of type B to perform a specific action. B would be available to A through an interface, not an implementation. Hence A would not know that (say) B1 is the implementation of B, but rather know about B alone

?

?

From: testdrivendevelopment@... [mailto:testdrivendevelopment@...] On Behalf Of David Burstin
Sent: Sunday, February 2, 2014 9:40 PM
To: testdrivendevelopment@...
Subject: Re: [TDD] How do I unit test a Dispose method ?

?

?

Amir, I feel like I might be missing your point.?

?

Why is it unreasonable for A to know that B uses expensive resources and provides a way to release them early? Surely this was one of the considerations when choosing B over X or Y or Z. IMHO it's a characteristic, not an implementation detail.

?

On 3 February 2014 15:19, Amir Kolsky <amir.kolsky@...> wrote:

?

?

Except that B must already be IDisposable. The IDisposable pattern requires that any object that owns an IDisposable must itself implement IDisposable.

?

n? This is inane, as it requires the owning object to know about implementation details of it¡¯s ownee.

I disagree. All that A needs to know is that B implements the IDisposable interface.

?

? * A has to know about something pertaining to B that has nothing to do with why A needs it.

?

?

?


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

 

Unless it's a factory

On Feb 3, 2014 6:34 PM, "Amir Kolsky" <amir.kolsky@...> wrote:

?

I agree, my point is that you should ALWAYS be given the object rather than it belonging to you. Therefore, the whole IDisposable pattern seems flawed from a design perspective.

?

All that said, you can still test drive a flawed design¡­. J

?

From: testdrivendevelopment@... [mailto:testdrivendevelopment@...] On Behalf Of Brad Wilson
Sent: Monday, February 3, 2014 7:48 AM
To: testdrivendevelopment@...
Subject: Re: [TDD] How do I unit test a Dispose method ?

?

?

...except, if you're given the object rather than creating it yourself, then its lifetime doesn't belong to you, and you don't need to be concerned with whether it's disposable.

?

On Sun, Feb 2, 2014 at 11:36 PM, Amir Kolsky <amir.kolsky@...> wrote:

?

If A cares about this specific property it would require B to implement IDisposable.

From A¡¯s perspective, it is given an object of type B to perform a specific action. B would be available to A through an interface, not an implementation. Hence A would not know that (say) B1 is the implementation of B, but rather know about B alone

?

?

From: testdrivendevelopment@... [mailto:testdrivendevelopment@...] On Behalf Of David Burstin
Sent: Sunday, February 2, 2014 9:40 PM
To: testdrivendevelopment@...
Subject: Re: [TDD] How do I unit test a Dispose method ?

?

?

Amir, I feel like I might be missing your point.?

?

Why is it unreasonable for A to know that B uses expensive resources and provides a way to release them early? Surely this was one of the considerations when choosing B over X or Y or Z. IMHO it's a characteristic, not an implementation detail.

?

On 3 February 2014 15:19, Amir Kolsky <amir.kolsky@...> wrote:

?

?

Except that B must already be IDisposable. The IDisposable pattern requires that any object that owns an IDisposable must itself implement IDisposable.

?

n? This is inane, as it requires the owning object to know about implementation details of it¡¯s ownee.

I disagree. All that A needs to know is that B implements the IDisposable interface.

?

? * A has to know about something pertaining to B that has nothing to do with why A needs it.

?

?

?


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

 

¿ªÔÆÌåÓý

I agree, my point is that you should ALWAYS be given the object rather than it belonging to you. Therefore, the whole IDisposable pattern seems flawed from a design perspective.

?

All that said, you can still test drive a flawed design¡­. J

?

From: testdrivendevelopment@... [mailto:testdrivendevelopment@...] On Behalf Of Brad Wilson
Sent: Monday, February 3, 2014 7:48 AM
To: testdrivendevelopment@...
Subject: Re: [TDD] How do I unit test a Dispose method ?

?

?

...except, if you're given the object rather than creating it yourself, then its lifetime doesn't belong to you, and you don't need to be concerned with whether it's disposable.

?

On Sun, Feb 2, 2014 at 11:36 PM, Amir Kolsky <amir.kolsky@...> wrote:

?

If A cares about this specific property it would require B to implement IDisposable.

From A¡¯s perspective, it is given an object of type B to perform a specific action. B would be available to A through an interface, not an implementation. Hence A would not know that (say) B1 is the implementation of B, but rather know about B alone

?

?

From: testdrivendevelopment@... [mailto:testdrivendevelopment@...] On Behalf Of David Burstin
Sent: Sunday, February 2, 2014 9:40 PM
To: testdrivendevelopment@...
Subject: Re: [TDD] How do I unit test a Dispose method ?

?

?

Amir, I feel like I might be missing your point.?

?

Why is it unreasonable for A to know that B uses expensive resources and provides a way to release them early? Surely this was one of the considerations when choosing B over X or Y or Z. IMHO it's a characteristic, not an implementation detail.

?

On 3 February 2014 15:19, Amir Kolsky <amir.kolsky@...> wrote:

?

?

Except that B must already be IDisposable. The IDisposable pattern requires that any object that owns an IDisposable must itself implement IDisposable.

?

n? This is inane, as it requires the owning object to know about implementation details of it¡¯s ownee.

I disagree. All that A needs to know is that B implements the IDisposable interface.

?

? * A has to know about something pertaining to B that has nothing to do with why A needs it.

?

?

?


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

Brad Wilson
 

...except, if you're given the object rather than creating it yourself, then its lifetime doesn't belong to you, and you don't need to be concerned with whether it's disposable.


On Sun, Feb 2, 2014 at 11:36 PM, Amir Kolsky <amir.kolsky@...> wrote:


If A cares about this specific property it would require B to implement IDisposable.

From A¡¯s perspective, it is given an object of type B to perform a specific action. B would be available to A through an interface, not an implementation. Hence A would not know that (say) B1 is the implementation of B, but rather know about B alone

?

?

From: testdrivendevelopment@... [mailto:testdrivendevelopment@...] On Behalf Of David Burstin
Sent: Sunday, February 2, 2014 9:40 PM
To: testdrivendevelopment@...
Subject: Re: [TDD] How do I unit test a Dispose method ?

?

?

Amir, I feel like I might be missing your point.?

?

Why is it unreasonable for A to know that B uses expensive resources and provides a way to release them early? Surely this was one of the considerations when choosing B over X or Y or Z. IMHO it's a characteristic, not an implementation detail.

?

On 3 February 2014 15:19, Amir Kolsky <amir.kolsky@...> wrote:

?

?

Except that B must already be IDisposable. The IDisposable pattern requires that any object that owns an IDisposable must itself implement IDisposable.

?

n? This is inane, as it requires the owning object to know about implementation details of it¡¯s ownee.

I disagree. All that A needs to know is that B implements the IDisposable interface.

?

? * A has to know about something pertaining to B that has nothing to do with why A needs it.

?





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

 

¿ªÔÆÌåÓý

If A cares about this specific property it would require B to implement IDisposable.

From A¡¯s perspective, it is given an object of type B to perform a specific action. B would be available to A through an interface, not an implementation. Hence A would not know that (say) B1 is the implementation of B, but rather know about B alone

?

?

From: testdrivendevelopment@... [mailto:testdrivendevelopment@...] On Behalf Of David Burstin
Sent: Sunday, February 2, 2014 9:40 PM
To: testdrivendevelopment@...
Subject: Re: [TDD] How do I unit test a Dispose method ?

?

?

Amir, I feel like I might be missing your point.?

?

Why is it unreasonable for A to know that B uses expensive resources and provides a way to release them early? Surely this was one of the considerations when choosing B over X or Y or Z. IMHO it's a characteristic, not an implementation detail.

?

On 3 February 2014 15:19, Amir Kolsky <amir.kolsky@...> wrote:

?

?

Except that B must already be IDisposable. The IDisposable pattern requires that any object that owns an IDisposable must itself implement IDisposable.

?

n? This is inane, as it requires the owning object to know about implementation details of it¡¯s ownee.

I disagree. All that A needs to know is that B implements the IDisposable interface.

?

? * A has to know about something pertaining to B that has nothing to do with why A needs it.

?


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

 

Amir, I feel like I might be missing your point.?

Why is it unreasonable for A to know that B uses expensive resources and provides a way to release them early? Surely this was one of the considerations when choosing B over X or Y or Z. IMHO it's a characteristic, not an implementation detail.


On 3 February 2014 15:19, Amir Kolsky <amir.kolsky@...> wrote:
?

?

Except that B must already be IDisposable. The IDisposable pattern requires that any object that owns an IDisposable must itself implement IDisposable.

?

n? This is inane, as it requires the owning object to know about implementation details of it¡¯s ownee.

I disagree. All that A needs to know is that B implements the IDisposable interface.

?

? * A has to know about something pertaining to B that has nothing to do with why A needs it.



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

 

¿ªÔÆÌåÓý

?

Except that B must already be IDisposable. The IDisposable pattern requires that any object that owns an IDisposable must itself implement IDisposable.

?

n? This is inane, as it requires the owning object to know about implementation details of it¡¯s ownee.

I disagree. All that A needs to know is that B implements the IDisposable interface.

?

? * A has to know about something pertaining to B that has nothing to do with why A needs it.


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

 

On 3 February 2014 14:48, Amir Kolsky <amir.kolsky@...> wrote:

?

Except that B must already be IDisposable. The IDisposable pattern requires that any object that owns an IDisposable must itself implement IDisposable.

?

n? This is inane, as it requires the owning object to know about implementation details of it¡¯s ownee.

I disagree. All that A needs to know is that B implements the IDisposable interface.

?

?

I agree completely. My original point was about not injecting B and then disposing of it.

??????????????? So how would the original creator of B know when to dispose of it? And given that separation of use from construction is dogmatic, you will always encounter this problem¡­.


The only reason to ever call Dispose() is to initiate an early release of expensive internal resources. If the original creator of B is finished with B, then it should call B.Dispose(). If it isn't then it shouldn't. If unsure, don't call Dispose(),and?the garbage collector will eventually clean up your IDisposables anyway.

Given that this only applies to expensive resources, then ideally the tying up, use and release of those resources should be as close to each other as possible. Say B has an Open() and Close() method, and Close() calls Dispose() internally, B can still be injected and still implement IDisposable. It's construction and use can still be separated.


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

 




On 3 February 2014 14:39, Amir Kolsky <amir.kolsky@...> wrote:
?

This only applies to objects that it owns. Otherwise if it dispose of a reference that has been passed to it (say through injection) then it may be closing an object that is still required elsewhere.

?

Which is the problem with IDisposable to begin with. It is well-nigh impossible to define the behavior properly.


I agree that it is difficult. Here is the test list that I used:

  • when the Finalizer is called it releases all unmanaged resources
  • when the Finalizer is called it does NOT Dispose its managed resources (remember that the order of finalization is not guaranteed, so the managed resource may already have been finalized)
  • when?Dispose()?is called all unmanaged resources are released
  • when?Dispose()?is called all managed Disposable objects are Disposed

To conform to the, the target class must also:

  • implement IDisposable
  • provide a?protected virtual Dispose(bool disposing)?method
  • the?Dispose()?method must call?Dispose(true)
  • the?Dispose()?method must suppress finalization
  • if a finalizer is needed, it must call?Dispose(false)
  • handle?Dispose()?being called multiple times
  • throw an ObjectDisposedException in any other public method tries to access disposed objects

Some of these were challenging (or impossible) to write automated tests for, some were just messy.


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

 

¿ªÔÆÌåÓý

?

Except that B must already be IDisposable. The IDisposable pattern requires that any object that owns an IDisposable must itself implement IDisposable.

?

n? This is inane, as it requires the owning object to know about implementation details of it¡¯s ownee.

?

I agree completely. My original point was about not injecting B and then disposing of it.

??????????????? So how would the original creator of B know when to dispose of it? And given that separation of use from construction is dogmatic, you will always encounter this problem¡­.

?

?

A.

?

From: testdrivendevelopment@... [mailto:testdrivendevelopment@...] On Behalf Of David Burstin
Sent: Sunday, February 2, 2014 7:33 PM


To: testdrivendevelopment@...
Subject: Re: [TDD] How do I unit test a Dispose method ?

?

?

It depends.

?

If its Dispose() method has been called (its owner has requested early release of its resources), then it is required to call Dispose() on all of its IDisposable objects.

?

However, if its Dispose() method is not called then cleanup is ultimately initiated by the GC through the Finalization Queue. As the order of finalization is not guaranteed, the object must not call Dispose() on any of its IDisposable objects.

?

On 3 February 2014 14:28, Amir Kolsky <amir.kolsky@...> wrote:

?

Should an IDisposable object call Dispose on the IDisposable objects that it holds?

?

From: testdrivendevelopment@... [mailto:testdrivendevelopment@...] On Behalf Of David Burstin
Sent: Sunday, February 2, 2014 7:25 PM
To: testdrivendevelopment@...


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

?

?

Hi all. Long time listener, first time caller :)

?

I've just finished a blog post called?

?

As IDisposable is primarily about managing internal resources, I agree with Amir that the best approach is to subclass, which allows us to spy on those resources. But I would not inject the resources to spy on as only the owner of the resource is responsible for its disposal - in this case the owner would be the test, not the SUT.

?

?

?

On 24 January 2014 19:28, Donaldson, John <john.m.donaldson@...> wrote:

?

.a. Yes, you are right it's an implementation detail.
Would another way make things easier?

For example, separate the storing of the password from the Connection.
You might delegate to the PasswordStore to hold and remove passwords.
Then you just need to show that:
- the PasswordStore is wired up to the Connection
- the PasswordStore does what you want.

John D.



-----Original Message-----
From: testdrivendevelopment@... [mailto:testdrivendevelopment@...] On Behalf Of Amir Kolsky
Sent: 24 January 2014 03:07
To: testdrivendevelopment@...
Subject: RE: [TDD] How do I unit test a Dispose method ?

Here's another way of thinking about this - Whether a member is IDisposable or not is really an implementation issue (as which members comprise an object is private).
So the general question here is how do we get an object to identify all of its IDisposable private methods and invoke Dispose on them in its Dispose.
There are several ways I can think of doing this, all of which are sort of terrible, but then, we're dealing with a true implementation specific problem.

The underlying technique in all cases is going to be the injection of two more members which are IDisposable mocks.

We can try to .emit code at runtime that will add these mocks to the UUT, but - ugh!
Partial classes could work, but then we'd have to mark the original as partial, so - ugh!
The easiest is to subclass the original class and have the (mock) subclass have two (mock) members whose Dispose() must be called. The implementation would, naturally, have to use reflection to find all members and Dispose() of them properly, but that's what we want to see anyway.

The price to pay is that the class cannot be sealed, but that is really not a problem as only API classes should be sealed anyway and if you class serves both as an API and it actually does anything, shame on you :)

.a.

From: testdrivendevelopment@...testdrivendevelopment@...> [mailto:testdrivendevelopment@...] On Behalf Of Roy Osherove
Sent: Thursday, January 23, 2014 4:18 AM
To: testdrivendevelopment
Subject: 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@...<mailto: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

- @RoyOsherove<>
- Read my new book Notes to a Software Team Leader<>
- My blog for team leaders:
- +47-96-90-22-15<tel:%2B47-96-90-22-15> (Oslo Time)

[Non-text portions of this message have been removed]

------------------------------------

Yahoo Groups Links

?

?

?


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

 

On 3 February 2014 14:37, Amir Kolsky <amir.kolsky@...> wrote:

?

That is what I thought. So I object A uses B and C and C (and A) are IDisposable, we need to ascertain that C¡¯s Dispose is called, right?

?

Note, however, that the fact that A uses B and C, is an implementation detail and as such in our test we probably do not want to deal with C explicitly. What happens, for example, if B suddenly becomes IDisposable?


Except that B must already be IDisposable. The IDisposable pattern requires that any object that owns an IDisposable must itself implement IDisposable.
?

?

This is why I said that the approach should be generic, so that A will say ¡°since I am IDisposable, I will make sure that any of my members which are also IDiposable will be Disposed of.¡± In place of saying ¡°I will Dispose of C.¡±


I agree completely. My original point was about not injecting B and then disposing of it.
?

?

A.

?

From: testdrivendevelopment@... [mailto:testdrivendevelopment@...] On Behalf Of David Burstin
Sent: Sunday, February 2, 2014 7:33 PM


To: testdrivendevelopment@...
Subject: Re: [TDD] How do I unit test a Dispose method ?

?

?

It depends.

?

If its Dispose() method has been called (its owner has requested early release of its resources), then it is required to call Dispose() on all of its IDisposable objects.

?

However, if its Dispose() method is not called then cleanup is ultimately initiated by the GC through the Finalization Queue. As the order of finalization is not guaranteed, the object must not call Dispose() on any of its IDisposable objects.

?

On 3 February 2014 14:28, Amir Kolsky <amir.kolsky@...> wrote:

?

Should an IDisposable object call Dispose on the IDisposable objects that it holds?

?

From: testdrivendevelopment@... [mailto:testdrivendevelopment@...] On Behalf Of David Burstin
Sent: Sunday, February 2, 2014 7:25 PM
To: testdrivendevelopment@...


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

?

?

Hi all. Long time listener, first time caller :)

?

I've just finished a blog post called?

?

As IDisposable is primarily about managing internal resources, I agree with Amir that the best approach is to subclass, which allows us to spy on those resources. But I would not inject the resources to spy on as only the owner of the resource is responsible for its disposal - in this case the owner would be the test, not the SUT.

?

?

?

On 24 January 2014 19:28, Donaldson, John <john.m.donaldson@...> wrote:

?

.a. Yes, you are right it's an implementation detail.
Would another way make things easier?

For example, separate the storing of the password from the Connection.
You might delegate to the PasswordStore to hold and remove passwords.
Then you just need to show that:
- the PasswordStore is wired up to the Connection
- the PasswordStore does what you want.

John D.



-----Original Message-----
From: testdrivendevelopment@... [mailto:testdrivendevelopment@...] On Behalf Of Amir Kolsky
Sent: 24 January 2014 03:07
To: testdrivendevelopment@...
Subject: RE: [TDD] How do I unit test a Dispose method ?

Here's another way of thinking about this - Whether a member is IDisposable or not is really an implementation issue (as which members comprise an object is private).
So the general question here is how do we get an object to identify all of its IDisposable private methods and invoke Dispose on them in its Dispose.
There are several ways I can think of doing this, all of which are sort of terrible, but then, we're dealing with a true implementation specific problem.

The underlying technique in all cases is going to be the injection of two more members which are IDisposable mocks.

We can try to .emit code at runtime that will add these mocks to the UUT, but - ugh!
Partial classes could work, but then we'd have to mark the original as partial, so - ugh!
The easiest is to subclass the original class and have the (mock) subclass have two (mock) members whose Dispose() must be called. The implementation would, naturally, have to use reflection to find all members and Dispose() of them properly, but that's what we want to see anyway.

The price to pay is that the class cannot be sealed, but that is really not a problem as only API classes should be sealed anyway and if you class serves both as an API and it actually does anything, shame on you :)

.a.

From: testdrivendevelopment@...testdrivendevelopment@...> [mailto:testdrivendevelopment@...] On Behalf Of Roy Osherove
Sent: Thursday, January 23, 2014 4:18 AM
To: testdrivendevelopment
Subject: 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@...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

- @RoyOsherove<>
- Read my new book Notes to a Software Team Leader<>
- My blog for team leaders:
- +47-96-90-22-15<tel:%2B47-96-90-22-15> (Oslo Time)

[Non-text portions of this message have been removed]

------------------------------------

Yahoo Groups Links

?

?



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

 

¿ªÔÆÌåÓý

This only applies to objects that it owns. Otherwise if it dispose of a reference that has been passed to it (say through injection) then it may be closing an object that is still required elsewhere.

?

Which is the problem with IDisposable to begin with. It is well-nigh impossible to define the behavior properly.

?

?

On 3 February 2014 14:33, David Burstin <david.burstin@...> wrote:

It depends.

?

If its Dispose() method has been called (its owner has requested early release of its resources), then it is required to call Dispose() on all of its IDisposable objects.

?

However, if its Dispose() method is not called then cleanup is ultimately initiated by the GC through the Finalization Queue. As the order of finalization is not guaranteed, the object must not call Dispose() on any of its IDisposable objects.

?

On 3 February 2014 14:28, Amir Kolsky <amir.kolsky@...> wrote:

?

Should an IDisposable object call Dispose on the IDisposable objects that it holds?

?

From: testdrivendevelopment@... [mailto:testdrivendevelopment@...] On Behalf Of David Burstin
Sent: Sunday, February 2, 2014 7:25 PM
To: testdrivendevelopment@...


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

?

?

Hi all. Long time listener, first time caller :)

?

I've just finished a blog post called?

?

As IDisposable is primarily about managing internal resources, I agree with Amir that the best approach is to subclass, which allows us to spy on those resources. But I would not inject the resources to spy on as only the owner of the resource is responsible for its disposal - in this case the owner would be the test, not the SUT.

?

?

?

On 24 January 2014 19:28, Donaldson, John <john.m.donaldson@...> wrote:

?

.a. Yes, you are right it's an implementation detail.
Would another way make things easier?

For example, separate the storing of the password from the Connection.
You might delegate to the PasswordStore to hold and remove passwords.
Then you just need to show that:
- the PasswordStore is wired up to the Connection
- the PasswordStore does what you want.

John D.



-----Original Message-----
From: testdrivendevelopment@... [mailto:testdrivendevelopment@...] On Behalf Of Amir Kolsky
Sent: 24 January 2014 03:07
To: testdrivendevelopment@...
Subject: RE: [TDD] How do I unit test a Dispose method ?

Here's another way of thinking about this - Whether a member is IDisposable or not is really an implementation issue (as which members comprise an object is private).
So the general question here is how do we get an object to identify all of its IDisposable private methods and invoke Dispose on them in its Dispose.
There are several ways I can think of doing this, all of which are sort of terrible, but then, we're dealing with a true implementation specific problem.

The underlying technique in all cases is going to be the injection of two more members which are IDisposable mocks.

We can try to .emit code at runtime that will add these mocks to the UUT, but - ugh!
Partial classes could work, but then we'd have to mark the original as partial, so - ugh!
The easiest is to subclass the original class and have the (mock) subclass have two (mock) members whose Dispose() must be called. The implementation would, naturally, have to use reflection to find all members and Dispose() of them properly, but that's what we want to see anyway.

The price to pay is that the class cannot be sealed, but that is really not a problem as only API classes should be sealed anyway and if you class serves both as an API and it actually does anything, shame on you :)

.a.

From: testdrivendevelopment@...testdrivendevelopment@...> [mailto:testdrivendevelopment@...] On Behalf Of Roy Osherove
Sent: Thursday, January 23, 2014 4:18 AM
To: testdrivendevelopment
Subject: 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@...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

- @RoyOsherove<>
- Read my new book Notes to a Software Team Leader<>
- My blog for team leaders:
- +47-96-90-22-15<tel:%2B47-96-90-22-15> (Oslo Time)

[Non-text portions of this message have been removed]

------------------------------------

Yahoo Groups Links

?

?

?


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

 

¿ªÔÆÌåÓý

That is what I thought. So I object A uses B and C and C (and A) are IDisposable, we need to ascertain that C¡¯s Dispose is called, right?

?

Note, however, that the fact that A uses B and C, is an implementation detail and as such in our test we probably do not want to deal with C explicitly. What happens, for example, if B suddenly becomes IDisposable?

?

This is why I said that the approach should be generic, so that A will say ¡°since I am IDisposable, I will make sure that any of my members which are also IDiposable will be Disposed of.¡± In place of saying ¡°I will Dispose of C.¡±

?

A.

?

From: testdrivendevelopment@... [mailto:testdrivendevelopment@...] On Behalf Of David Burstin
Sent: Sunday, February 2, 2014 7:33 PM
To: testdrivendevelopment@...
Subject: Re: [TDD] How do I unit test a Dispose method ?

?

?

It depends.

?

If its Dispose() method has been called (its owner has requested early release of its resources), then it is required to call Dispose() on all of its IDisposable objects.

?

However, if its Dispose() method is not called then cleanup is ultimately initiated by the GC through the Finalization Queue. As the order of finalization is not guaranteed, the object must not call Dispose() on any of its IDisposable objects.

?

On 3 February 2014 14:28, Amir Kolsky <amir.kolsky@...> wrote:

?

Should an IDisposable object call Dispose on the IDisposable objects that it holds?

?

From: testdrivendevelopment@... [mailto:testdrivendevelopment@...] On Behalf Of David Burstin
Sent: Sunday, February 2, 2014 7:25 PM
To: testdrivendevelopment@...


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

?

?

Hi all. Long time listener, first time caller :)

?

I've just finished a blog post called?

?

As IDisposable is primarily about managing internal resources, I agree with Amir that the best approach is to subclass, which allows us to spy on those resources. But I would not inject the resources to spy on as only the owner of the resource is responsible for its disposal - in this case the owner would be the test, not the SUT.

?

?

?

On 24 January 2014 19:28, Donaldson, John <john.m.donaldson@...> wrote:

?

.a. Yes, you are right it's an implementation detail.
Would another way make things easier?

For example, separate the storing of the password from the Connection.
You might delegate to the PasswordStore to hold and remove passwords.
Then you just need to show that:
- the PasswordStore is wired up to the Connection
- the PasswordStore does what you want.

John D.



-----Original Message-----
From: testdrivendevelopment@... [mailto:testdrivendevelopment@...] On Behalf Of Amir Kolsky
Sent: 24 January 2014 03:07
To: testdrivendevelopment@...
Subject: RE: [TDD] How do I unit test a Dispose method ?

Here's another way of thinking about this - Whether a member is IDisposable or not is really an implementation issue (as which members comprise an object is private).
So the general question here is how do we get an object to identify all of its IDisposable private methods and invoke Dispose on them in its Dispose.
There are several ways I can think of doing this, all of which are sort of terrible, but then, we're dealing with a true implementation specific problem.

The underlying technique in all cases is going to be the injection of two more members which are IDisposable mocks.

We can try to .emit code at runtime that will add these mocks to the UUT, but - ugh!
Partial classes could work, but then we'd have to mark the original as partial, so - ugh!
The easiest is to subclass the original class and have the (mock) subclass have two (mock) members whose Dispose() must be called. The implementation would, naturally, have to use reflection to find all members and Dispose() of them properly, but that's what we want to see anyway.

The price to pay is that the class cannot be sealed, but that is really not a problem as only API classes should be sealed anyway and if you class serves both as an API and it actually does anything, shame on you :)

.a.

From: testdrivendevelopment@...testdrivendevelopment@...> [mailto:testdrivendevelopment@...] On Behalf Of Roy Osherove
Sent: Thursday, January 23, 2014 4:18 AM
To: testdrivendevelopment
Subject: 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@...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

- @RoyOsherove<>
- Read my new book Notes to a Software Team Leader<>
- My blog for team leaders:
- +47-96-90-22-15<tel:%2B47-96-90-22-15> (Oslo Time)

[Non-text portions of this message have been removed]

------------------------------------

Yahoo Groups Links

?

?


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

 

Sorry - hit send too early:

It depends.

If its Dispose() method has been called (its owner has requested early release of its resources), then it is required to call Dispose() on all of its?IDisposable?objects.

However, if its Dispose() method is not called then cleanup is ultimately initiated by the GC through the Finalization Queue. As the order of finalization is not guaranteed, the object must not call Dispose() on any of its?IDisposable?objects.

This only applies to objects that it owns. Otherwise if it dispose of a reference that has been passed to it (say through injection) then it may be closing an object that is still required elsewhere.


On 3 February 2014 14:33, David Burstin <david.burstin@...> wrote:
It depends.

If its Dispose() method has been called (its owner has requested early release of its resources), then it is required to call Dispose() on all of its IDisposable objects.

However, if its Dispose() method is not called then cleanup is ultimately initiated by the GC through the Finalization Queue. As the order of finalization is not guaranteed, the object must not call Dispose() on any of its IDisposable objects.


On 3 February 2014 14:28, Amir Kolsky <amir.kolsky@...> wrote:
?

Should an IDisposable object call Dispose on the IDisposable objects that it holds?

?

From: testdrivendevelopment@... [mailto:testdrivendevelopment@...] On Behalf Of David Burstin
Sent: Sunday, February 2, 2014 7:25 PM
To: testdrivendevelopment@...


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

?

?

Hi all. Long time listener, first time caller :)

?

I've just finished a blog post called?

?

As IDisposable is primarily about managing internal resources, I agree with Amir that the best approach is to subclass, which allows us to spy on those resources. But I would not inject the resources to spy on as only the owner of the resource is responsible for its disposal - in this case the owner would be the test, not the SUT.

?

?

?

On 24 January 2014 19:28, Donaldson, John <john.m.donaldson@...> wrote:

?

.a. Yes, you are right it's an implementation detail.
Would another way make things easier?

For example, separate the storing of the password from the Connection.
You might delegate to the PasswordStore to hold and remove passwords.
Then you just need to show that:
- the PasswordStore is wired up to the Connection
- the PasswordStore does what you want.

John D.



-----Original Message-----
From: testdrivendevelopment@... [mailto:testdrivendevelopment@...] On Behalf Of Amir Kolsky
Sent: 24 January 2014 03:07
To: testdrivendevelopment@...
Subject: RE: [TDD] How do I unit test a Dispose method ?

Here's another way of thinking about this - Whether a member is IDisposable or not is really an implementation issue (as which members comprise an object is private).
So the general question here is how do we get an object to identify all of its IDisposable private methods and invoke Dispose on them in its Dispose.
There are several ways I can think of doing this, all of which are sort of terrible, but then, we're dealing with a true implementation specific problem.

The underlying technique in all cases is going to be the injection of two more members which are IDisposable mocks.

We can try to .emit code at runtime that will add these mocks to the UUT, but - ugh!
Partial classes could work, but then we'd have to mark the original as partial, so - ugh!
The easiest is to subclass the original class and have the (mock) subclass have two (mock) members whose Dispose() must be called. The implementation would, naturally, have to use reflection to find all members and Dispose() of them properly, but that's what we want to see anyway.

The price to pay is that the class cannot be sealed, but that is really not a problem as only API classes should be sealed anyway and if you class serves both as an API and it actually does anything, shame on you :)

.a.

From: testdrivendevelopment@...testdrivendevelopment@...> [mailto:testdrivendevelopment@...] On Behalf Of Roy Osherove
Sent: Thursday, January 23, 2014 4:18 AM
To: testdrivendevelopment
Subject: 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@...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

- @RoyOsherove<>
- Read my new book Notes to a Software Team Leader<>
- My blog for team leaders:
- +47-96-90-22-15<tel:%2B47-96-90-22-15> (Oslo Time)

[Non-text portions of this message have been removed]

------------------------------------

Yahoo Groups Links

?