¿ªÔÆÌåÓý

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

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 ?

 

¿ªÔÆÌåÓý

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 ?

 

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 (Oslo Time)

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

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

Yahoo Groups Links



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

Donaldson, John
 

.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@...<mailto: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 ?

 

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@...<mailto: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)


Re: Single assert / Single mock rule in practice

 

Some more links concerning the? Command Query separation issue:

Sandy Metz had a nice explanation ans slides: http://www.youtube.com/watch?v=qPfQM4w4I04&t=12m54s

Similar post by Mark Seemann: http://blog.ploeh.dk/2013/10/23/mocks-for-commands-stubs-for-queries/


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)



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

 

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



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

 

¿ªÔÆÌåÓý

A method on Connection: hasPassword?
R
On Jan 23, 2014, at 2:26 AM, Gishu Pillai <gishu.pillai@...> wrote:

Does anyone have something better ?


Ron Jeffries
You never know what is enough unless you know what is more than enough. -- William Blake


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

Donaldson, John
 

¿ªÔÆÌåÓý

Gishu,

?

Could you inject a mocked SecureString?

Then you can check if the SecureString.Dispose is called.

Seems like a good place to use a mock as it¡¯s a ¡°tell¡± pattern.

?

John D.

?

From: testdrivendevelopment@... [mailto:testdrivendevelopment@...] On Behalf Of Gishu Pillai
Sent: 23 January 2014 08:26
To: testdrivendevelopment@...
Subject: [TDD] How do I unit test a Dispose method ?

?




The question is a bit .Net specific.
Assuming the simple case, where I have a type which wraps a internal IDisposable member.

class Connection
{

?? private SecureString password;

}

SecureString is Disposable. That mandates that Connection should have a Dispose method, which should dispose its members.

Options:
* To know whether the internal member has been disposed, I'd have to expose it (so that the unit test can see it). Doesn't feel right to me.

* The other hack would be to promote it from private to internal + use the InternalVisibleTo path to access it.

Does anyone have something better ?

Gishu





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

 

How does a connection get its SecureString? Can you inject one for test purposes, saving a reference to it?

Charlie


On Wed, Jan 22, 2014 at 11:26 PM, Gishu Pillai <gishu.pillai@...> wrote:
?

The question is a bit .Net specific.
Assuming the simple case, where I have a type which wraps a internal IDisposable member.

class Connection
{
?? private SecureString password;
}

SecureString is Disposable. That mandates that Connection should have a Dispose method, which should dispose its members.

Options:
* To know whether the internal member has been disposed, I'd have to expose it (so that the unit test can see it). Doesn't feel right to me.
* The other hack would be to promote it from private to internal + use the InternalVisibleTo path to access it.

Does anyone have something better ?

Gishu



How do I unit test a Dispose method ?

 

The question is a bit .Net specific.
Assuming the simple case, where I have a type which wraps a internal IDisposable member.

class Connection
{
?? private SecureString password;
}

SecureString is Disposable. That mandates that Connection should have a Dispose method, which should dispose its members.

Options:
* To know whether the internal member has been disposed, I'd have to expose it (so that the unit test can see it). Doesn't feel right to me.
* The other hack would be to promote it from private to internal + use the InternalVisibleTo path to access it.

Does anyone have something better ?

Gishu


Re: [TDD] Re: Single assert / Single mock rule in practice

Mateusz ?oskot
 


On 22 January 2014 14:11, Angel Java Lopez <ajlopez2000@...> wrote:
Ummm.. it's not the same effect. My alternative implementation is REALLY used by the application
?
I misunderstood it then, I thought the sole purpose of such alternative implementation is to double/fake the real xdep, solely for testing.

Best regards,
--
Mateusz ??oskot,


Re: [TDD] Re: Single assert / Single mock rule in practice

 

Ummm.. it's not the same effect. My alternative implementation is REALLY used by the application (for example, in the weekly iteration deliverable), so no fixed return_value to set via mock magic. I'm using it (the alternative implementation) as an implementation that cover all the use cases (no more, no less). And, in general, it is created using TDD itself.


On Wed, Jan 22, 2014 at 10:55 AM, Mateusz ?oskot <mateusz@...> wrote:
?


On 17 January 2014 10:25, Angel Java Lopez <ajlopez2000@...> wrote:

Interesting thread!

Regarding
def exists(item_id):
? ? xdep.open() ?# opens some remote resource

? ? item = xdep.get_item(item_id)
? ? xdep.close()
? ? if item:
? ? ? ? return??== item_id
? ? else:
? ? ? ? return false

I usually write a xdep alternative implementation. I don't call it a mock, because is an implementation of the same service, but local. And I use it even in the final application (for first iteration, for demo purpose). You can build it using TDD, and then, inject it to the exists() test. It has no additional method like "expected" nor other mockist methods.

It makes sense to me, but if we forget Python's module name of unittest.mock uses word 'mock',
then the only difference at low level of how xdep double is implemented:
- you would write by hand an alternative implementation of xdep
- I use Mock class to automate things

# mock will generate implementation for me
self.xdep = mock.Mock()
# I ask the object of Mock class to 'implement get_item for me so it returns 1'
self.xdep.get_item.return_value = 1 ?

but net result (and effect) is the same, I believe.

The details how the 'magic' happens, let someone write by hand or let something generate it, are IMO not important for my question
which Roy has nicely captured in?

"I would use a STUB for a get_something if not faking it meant an integration test"

How the get_something is faked, that is a different kind of issue.


Best regards,
--
Mateusz ??oskot,



Re: [TDD] Re: Single assert / Single mock rule in practice

Mateusz ?oskot
 


On 17 January 2014 10:25, Angel Java Lopez <ajlopez2000@...> wrote:

Interesting thread!

Regarding
def exists(item_id):
? ? xdep.open() ?# opens some remote resource

? ? item = xdep.get_item(item_id)
? ? xdep.close()
? ? if item:
? ? ? ? return??== item_id
? ? else:
? ? ? ? return false

I usually write a xdep alternative implementation. I don't call it a mock, because is an implementation of the same service, but local. And I use it even in the final application (for first iteration, for demo purpose). You can build it using TDD, and then, inject it to the exists() test. It has no additional method like "expected" nor other mockist methods.

It makes sense to me, but if we forget Python's module name of unittest.mock uses word 'mock',
then the only difference at low level of how xdep double is implemented:
- you would write by hand an alternative implementation of xdep
- I use Mock class to automate things

# mock will generate implementation for me
self.xdep = mock.Mock()
# I ask the object of Mock class to 'implement get_item for me so it returns 1'
self.xdep.get_item.return_value = 1 ?

but net result (and effect) is the same, I believe.

The details how the 'magic' happens, let someone write by hand or let something generate it, are IMO not important for my question
which Roy has nicely captured in?

"I would use a STUB for a get_something if not faking it meant an integration test"

How the get_something is faked, that is a different kind of issue.

Best regards,
--
Mateusz ??oskot,


Re: [TDD] Single assert / Single mock rule in practice

Mateusz ?oskot
 

On 16 January 2014 10:33, Roy Osherove <roy@...> wrote:
For example, I would use a STUB for a get_something if not faking it meant an integration test.?
I would not use a mock for it, since it is giving info back into the application.
I would use a mock only if there an is a "tell" operation on a collaborator as an end result.?
stubs: queries
mocks: commands
but only when you have no other choice but to fake it. I would rather test state or a return value and have only real classes. Mocks make tests complicated.

Roy,

Thanks for the excellent clarification, it helps to grasp other resources of yours that I've had pleasure to read/watch.

Best regards,
--
Mateusz ??oskot,


Re: [TDD] Re: Single assert / Single mock rule in practice

 

Interesting thread!

Regarding
def exists(item_id):
? ? xdep.open() ?# opens some remote resource

? ? item = xdep.get_item(item_id)
? ? xdep.close()
? ? if item:
? ? ? ? return??== item_id
? ? else:
? ? ? ? return false

I usually write a xdep alternative implementation. I don't call it a mock, because is an implementation of the same service, but local. And I use it even in the final application (for first iteration, for demo purpose). You can build it using TDD, and then, inject it to the exists() test. It has no additional method like "expected" nor other mockist methods.

In a non-public project, my team was building system A, that should call already existing system B (a REST API). Instead of struggling with test that directly call the REST API, we implemented a local C1 library, that provides all the service needed by A. That is:

A -> C1 and C1 works locally

Then, we write a C2 library that real calls the system B, using TDD:

C2 -> B

With A -> C1 we could build a lot of use cases, without network connection, REST API server running, etc, in an agile way.
Only in C2 -> B we needed such environment. Then, one day, we switched to

A -> C2 -> B

and voila! Practically all use cases were working.

But all C1 was not one or more mocks, it was an alternative and working implementaiton, in-memory.

Angel "Java" Lopez
@ajlopez
?





On Fri, Jan 17, 2014 at 6:36 AM, Mateusz ?oskot <mateusz@...> wrote:

?


On 15 January 2014 20:08, Adam Sroka <adam.sroka@...> wrote:
On Wed, Jan 15, 2014 at 12:56 PM, Mateusz ?oskot <mateusz@...> wrote:
On 15 January 2014 04:41, Adam Sroka <adam.sroka@...> wrote:
On Tue, Jan 14, 2014 at 5:31 PM, Mateusz ?oskot <mateusz@...> wrote:

Isn't a unit test somewhat tightly coupled to implementation?


Yes, by definition, because you are testing the way the "unit" works. It is necessarily coupled to the interface of the object you are testing, though not necessarily to its internals. If the test is coupled to the internals of the object that is a failure of encapsulation and there is probably a detectable code smell.?

Yes, that is waht a general theory of (unit) testing says, but...devil's in the detail like
classicists vs mockists ()
I mean, lots of things about unit teststing makes a perfect common sense and is obvious... from high altitude,
but practice seems to be much more complex.



It gets easier over time if you practice merciless refactoring. The problem is that most programmers don't. They change stuff when it is obvious and makes their lives immediately easier and defer it otherwise. Then it turns into a mess and they no longer know how to fix it easily.?
[...]

Adam,

I see your point, although it does not mean I immediately see where to plug it in my practice.
?
When and whether to use mocks is more a matter of style than anything else, and philosophical debates about it are mostly a waste of time.?


I do not mean to start any such debate, no.

I am learning from many resources what you are saying, when/if to mock is matter of style and I have no reason to not to trust it is.

But, as soon as I get back to practising UT
and I have an external dependency, i.e. remote service, mocking an obvious choice
otherwise I can't see how I can continue testing my unit without either integrating
with real remote service or mocking it.
Then I remember "mocking is matter of style" and I get confused immediately:
does it mean I either test my unit with mocking or not test at all?

I hope you can trace the tracks of my thinking here.

The a self-contained complete example I posted in which I'm
trying to test xdep as external legacy, uses code
beyond my control. I can't change how xdep is designed.

Let's try to simply it to get away from conceptual discussion on?
techniques and methods, and get down to bare code:

def exists(item_id):
? ? xdep.open() ?# opens some remote resource

? ? item = xdep.get_item(item_id)
? ? xdep.close()
? ? if item:
? ? ? ? return??== item_id
? ? else:
? ? ? ? return false

Given id of an existing item,?
When I call exists()
Then it reports item does exists (return True)

How would you test such function?

1. Mock the whole xdep (and set up all 3 functions as necessary for the scenario above).
2. Pick most relevant xdep function, namely the xdep.get_item and mock it?
3. No mocking at all? Then, how would you unit test exists function?

Best regards,
--
Mateusz ??oskot,



Re: [TDD] Single assert / Single mock rule in practice

Mateusz ?oskot
 

On 15 January 2014 20:20, Adam Sroka <adam.sroka@...> wrote:

Are you familiar with Command Query Separation (CQS)?

Sort of, related to Tell, Don't Ask?
I'll learn about it, thanks.

I've always found that the most coherent and useful way to explain why you would choose a mock or a stub. The most interesting thing about a method is either what it returns or, in the case where you don't care what it returns, whether it was called with the correct semantics, not both.?
So, if the SUT cares about the value that a method returns then you stub that method, only. If the SUT doesn't care about the value a method returns then you use a mock and verify the call has the expected semantics, only.?

?
That is something!
That actually answers my original question on what approach to choose
and how 'widely' to mock or stub things.


Does that help??

Yes, it is very helpful, thanks!,_._,___

Best regards,
--
Mateusz ??oskot,


Re: [TDD] Re: Single assert / Single mock rule in practice

Mateusz ?oskot
 


On 15 January 2014 20:08, Adam Sroka <adam.sroka@...> wrote:
On Wed, Jan 15, 2014 at 12:56 PM, Mateusz ?oskot <mateusz@...> wrote:
On 15 January 2014 04:41, Adam Sroka <adam.sroka@...> wrote:
On Tue, Jan 14, 2014 at 5:31 PM, Mateusz ?oskot <mateusz@...> wrote:

Isn't a unit test somewhat tightly coupled to implementation?


Yes, by definition, because you are testing the way the "unit" works. It is necessarily coupled to the interface of the object you are testing, though not necessarily to its internals. If the test is coupled to the internals of the object that is a failure of encapsulation and there is probably a detectable code smell.?

Yes, that is waht a general theory of (unit) testing says, but...devil's in the detail like
classicists vs mockists ()
I mean, lots of things about unit teststing makes a perfect common sense and is obvious... from high altitude,
but practice seems to be much more complex.



It gets easier over time if you practice merciless refactoring. The problem is that most programmers don't. They change stuff when it is obvious and makes their lives immediately easier and defer it otherwise. Then it turns into a mess and they no longer know how to fix it easily.?
[...]

Adam,

I see your point, although it does not mean I immediately see where to plug it in my practice.
?
When and whether to use mocks is more a matter of style than anything else, and philosophical debates about it are mostly a waste of time.?


I do not mean to start any such debate, no.

I am learning from many resources what you are saying, when/if to mock is matter of style and I have no reason to not to trust it is.

But, as soon as I get back to practising UT
and I have an external dependency, i.e. remote service, mocking an obvious choice
otherwise I can't see how I can continue testing my unit without either integrating
with real remote service or mocking it.
Then I remember "mocking is matter of style" and I get confused immediately:
does it mean I either test my unit with mocking or not test at all?

I hope you can trace the tracks of my thinking here.

The a self-contained complete example I posted in which I'm
trying to test xdep as external legacy, uses code
beyond my control. I can't change how xdep is designed.

Let's try to simply it to get away from conceptual discussion on?
techniques and methods, and get down to bare code:

def exists(item_id):
? ? xdep.open() ?# opens some remote resource
? ? item = xdep.get_item(item_id)
? ? xdep.close()
? ? if item:
? ? ? ? return??== item_id
? ? else:
? ? ? ? return false

Given id of an existing item,?
When I call exists()
Then it reports item does exists (return True)

How would you test such function?

1. Mock the whole xdep (and set up all 3 functions as necessary for the scenario above).
2. Pick most relevant xdep function, namely the xdep.get_item and mock it?
3. No mocking at all? Then, how would you unit test exists function?

Best regards,
--
Mateusz ??oskot,


Re: [TDD] Single assert / Single mock rule in practice

 

Here is how I define a unit of work (the thing under test) (from book chapter PDF:? )

Definition?
A unit of work is the sum of actions that take place between the invocation of a public
method in the system and a single noticeable end result by a test of that system. A
noticeable end result can be observed without looking at the internal state of the system and only through its public APIs and behavior. An end result is any of the following:
¡ö The invoked public method returns a value (a function that¡¯s not void).
¡ö There¡¯s a noticeable change to the state or behavior of the system before and
after invocation that can be determined without interrogating private state.
(Examples: the system can log in a previously nonexistent user, or the system¡¯s
properties change if the system is a state machine.)
¡ö There¡¯s a callout to a third-party system over which the test has no control, and
that third-party system doesn¡¯t return any value, or any return value from that
system is ignored. (Example: calling a third-party logging system that was not
written by you and you don¡¯t have the source to.)


END SNIP***

As far as I am concerned, the unit of work could span multiple classes or methods. if you have a dependency that is closely tied to the thing under test, and that you can run with you test in memory without faking it, I would try to not fake it. I would only use a mock where I had no other way but to prove a collaborator had to be called as an end result. For example, I would use a STUB for a get_something if not faking it meant an integration test. I would not use a mock for it, since it is giving info back into the application.
I would use a mock only if there an is a "tell" operation on a collaborator as an end result.?
stubs: queries
mocks: commands

but only when you have no other choice but to fake it. I would rather test state or a return value and have only real classes. Mocks make tests complicated.

My view is the opposite of the "mockist" approach wherein you use mocks to design the interaction protocol between classes via asserts. I find that approach leads to very hard to maintain tests over time.


On Wed, Jan 15, 2014 at 9:32 PM, Adam Sroka <adam.sroka@...> wrote:
?

P.S. in the first sentence of that second paragraph I should have said you stub the method and assert something about the SUT, only. In other words:

For a query:
Arrange: stub the query and pass the stub to the object under test
Act: call the method on the object under test that will cause the stubbed method to be queried
Assert: assert something about the object under test that should only be true if the stubbed method returns correctly

For a command:
Arrange: create a mock and pass it to the object under test *
Act: call the method on the object under test that will cause the method on the mock to be called
Assert: verify that the mock was called correctly which should only be true if the object under test did what we expected

* depending on the framework you may also have to specify how the mock is expected to be called before you call it.?



On Wed, Jan 15, 2014 at 3:20 PM, Adam Sroka <adam.sroka@...> wrote:
Are you familiar with Command Query Separation (CQS)? I've always found that the most coherent and useful way to explain why you would choose a mock or a stub. The most interesting thing about a method is either what it returns or, in the case where you don't care what it returns, whether it was called with the correct semantics, not both.?

So, if the SUT cares about the value that a method returns then you stub that method, only. If the SUT doesn't care about the value a method returns then you use a mock and verify the call has the expected semantics, only.?

Does that help??



On Tue, Jan 14, 2014 at 6:37 AM, Mateusz ?oskot <mateusz@...> wrote:
?

Hi,

I'd like to ask for ideas of best practices in case of developing and testing
a wrapper for a Python legacy module.
Especially, about applying the single assert / single mock rule
which I have been learning about from numerous books/videos/blogs.

I have Python xdep module which exposes API in form of bunch of free functions.
I'm creating new Python module foo which uses xdep (the idea is to provide
simplified, Pythonic interface, but that is not directly relevant to
my question here).

I'm trying to keep the code as simple & generic as possible, so my question is
not about Python but unit testing and mocking.

# xdep.py
def open():
...
def get_item(item_id):
...
def close():
...

# foo.py
import xdep

def exists(item_id):
xdep.open()
item = xdep.get_item(item_id)
xdep.close()
if item:
return == item_id
else:
return false

Here is my module to unit test behaviuour of the foo:
- I create and plug a stub for the legacy module xdep
- I mock functions of xdep

# test_foo.py
import unittest
import unittest.mock as mock

class ItemTest(unittest.TestCase):
def setUp(self):

# create stub for external dependency of foo
self.xdep_stub = mock.Mock()
patcher = mock.patch.dict('sys.modules', {'xdep': self.xdep_stub})
self.addCleanup(patcher.stop)
patcher.start()

# import foo does import xdep, as patched above
import foo
self.foo = foo

def test_exists_ExistingItemId_ReturnTrue(self):
foo = self.foo
# Arrange
### Data
item_id = 1
### Mock
foo.xdep.get_item.return_value = 1
# Act
item_exists = foo.item.exists(item_id)
# Assert
self.assertTrue(item_exists)

I have a single assertion and I use single mock which mocks xdep.get_item
function, so it seems a valid yet clear unit test.

Now, having the single assert/single mock rule in mind, I have some questions,
about the test_exists_ExistingItemId_ReturnTrue in particular:

1) How asserting on foo.item.exists return value is different from
asserting how xdep.get_item mock was called?

I could have replaced
self.assertTrue(item_exists)
with
foo.xdep.get_item.assert_called_once_with(1)

2) What about having both assertions, would that break the single
assertion rule?

self.assertTrue(item_exists)
foo.xdep.get_item.assert_called_once_with(1)

I'm not concern about breaking the rule as technical convention, but as:
am I testing multiple things here?

3) AFAIU, it's natural that unit tests are coupled with implementation of
method/behaviour they are testing, foo.exists function in my case.

So, given that foo.exists function calls three functions of the legacy
xdep module
xdep.open
xdep.get_item
xdep.close
should I mock all the three function and verify how they were called?
Would that still belong to scope of test_exists_ExistingItemId_ReturnTrue test
or I better create a new dedicated test case(s), something like
test_exists_IfCalled_xdepOpenCalled
test_exists_IfCalled_xdepCloseCalled
test_exists_IfCalled_xdepGetItemCalled
and do assert_called_once_with() appropriately.

Above, I count each mock for each xdep function as a separate mock,
so "single mock per test" rule seems to suggests I should verify they are called
in separate tests, doesn't it?

Also, some of other xdep functions need to be called with multiple parameters,
and as setting up and verifying their mocks may be quite elaborate,my
gut feeling
tells me that single unit test per "legacy API call expectation" as listed above
is a good approach for readability and maintainability of tests.
Am I right?

4) What other, if any, approach to single assert/single mock rules
would you take for testing similar case as above?

Best regards,
--
Mateusz ?oskot,






--
Thanks,

Roy Osherove

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