Keyboard Shortcuts
Likes
- Testdrivendevelopment
- Messages
Search
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:
|
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.
------------------------------------ ?
|
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:
|
Re: [TDD] How do I unit test a Dispose method ?
Donaldson, John
.a. Yes, you are right it's an implementation detail.
toggle quoted message
Show quoted text
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; _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:
-- Thanks,
Roy Osherove ?? - ? ?- Read my new book |
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. class Connection ?? private SecureString password; } SecureString is Disposable. That mandates that Connection should have a Dispose method, which should dispose its members. Options: * 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:
|
How do I unit test a Dispose method ?
The question is a bit .Net specific. class ConnectionAssuming the simple case, where I have a type which wraps a internal IDisposable member. { } 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: 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:
|
Re: [TDD] Re: Single assert / Single mock rule in practice
Mateusz ?oskot
On 17 January 2014 10:25, Angel Java Lopez <ajlopez2000@...> wrote:
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. 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: --
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 ?
|
Re: [TDD] Single assert / Single mock rule in practice
Mateusz ?oskot
On 15 January 2014 20:20, Adam Sroka <adam.sroka@...> wrote:
Sort of, related to Tell, Don't Ask? I'll learn about it, thanks.
? That is something! That actually answers my original question on what approach to choose and how 'widely' to mock or stub things.
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:
Adam, I see your point, although it does not mean I immediately see where to plug it in my practice. ?
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:
Thanks,
Roy Osherove ?? - ? ?- Read my new book ? ?- My blog for team leaders: ? ?- +47-96-90-22-15 (Oslo Time) |