Open gregjacobs opened 12 years ago
I'm very hesitant to add anything that encourages the use of verifyNoMoreInteractions
- it tends to encourage poor test practices. You can read a good blog about this here:
http://monkeyisland.pl/2008/07/12/should-i-worry-about-the-unexpected/
Have you tried using never
? That tends to be a better idea.
You can also read a good discussion of this for Mockito here: http://groups.google.com/group/mockito/browse_thread/thread/fefc46df7b65ce5f/bbe36216cddb2ce3#bbe36216cddb2ce3
All that said, I'll think about adding something of this sort. Patches are also appreciated.
How about, instead of resetting the recorded interactions to be able to create snapshots, from where then recorded interactions would start anew? That way, the history of the interactions would not be lost but rather kept in a backlog of recorded interaction snapshots.
This amounts to much the same thing, and is only necessary because of the use of verifyNoMoreInteractions
.
I'm suggesting that verifyNoMoreInteractions
is often not necessary, as the article and forum I referenced discuss. That said, there are some situations that could find both being useful - so I'll consider adding a reset.
I would personally say that the use of verifyNoMoreInteractions() is definitely situational. I don't often use it myself, but I have found a few cases where I do want to use it.
The one I came across when posting the issue was for a method that called other methods. I pretty much wanted to make sure that a certain dependency wasn't mutated (i.e. didn't get any more "tell" operations performed on it) than I expected.
I liked that article btw, and the distinction that he made about "ask" and "tell" operations.
It would be very useful to have a method which resets the interactions that have been recorded on a mock object, so that verifyNoMoreInteractions() can be used more effectively.
Consider the following scenario:
If I want to test myMethod() to see that dependentObj.setSomething() is called, my test may look something like this:
So what I propose is some way to reset the interactions that have been recorded on the mock. We could reset the interactions that the mock has recorded immediately after the MyClass instance has been created, so that we don't worry about what happened in the constructor. Ex: